cmdlets

What is a PowerShell cmdlet?

自古美人都是妖i 提交于 2019-12-03 17:20:37
问题 Approaching cmdlets in a conceptual way, How are they made? Are they compiled? Is it the equivalent of a batch file for PowerShell? Is it a script or a binary? What is the structure used for storing these cmdlets? 回答1: A PowerShell cmdlet is a compiled piece of .NET code, more precisely a single class if I am not mistaken. Cmdlets are kind of the "native" commands in PowerShell land, being able to handle object input and output as well as usually playing nice and well with the (object-based)

What is a PowerShell cmdlet?

醉酒当歌 提交于 2019-12-03 06:19:35
Approaching cmdlets in a conceptual way, How are they made? Are they compiled? Is it the equivalent of a batch file for PowerShell? Is it a script or a binary? What is the structure used for storing these cmdlets? A PowerShell cmdlet is a compiled piece of .NET code, more precisely a single class if I am not mistaken. Cmdlets are kind of the "native" commands in PowerShell land, being able to handle object input and output as well as usually playing nice and well with the (object-based) pipeline. Cmdlets have no direct representation in the file system, as they are not programs or similar.

How do I deal with Paths when writing a PowerShell Cmdlet?

不羁岁月 提交于 2019-12-03 00:55:16
问题 What is the proper way to receive a file as a parameter when writing a C# cmdlet? So far I just have a property LiteralPath (aligning with their parameter naming convention) that is a string. This is a problem because you just get whatever is typed into the console; which could be the full path or could be a relative path. Using Path.GetFullPath(string) doesn't work. It thinks I'm currently at ~, I'm not. Same problem occurs if I change the property from a string to a FileInfo. EDIT: For

Powershell Script to count .txt files only in a directory

孤者浪人 提交于 2019-12-02 16:47:49
问题 My goal is to send email to some users to notify about the current count of the files inside some folder directories. I need to count files only with .txt extensions and exclude folders and files inside this. please see below illustration U:\TESTING\Main Folder U:\TESTING\Main Folder\Sub Folder 1 U:\TESTING\Main Folder\Sub Folder 2 output result should look somehting like this Main Folder row should be 1 only, but it also includes the sub folders 1 and 2 and the txt files inside those. Here's

How do I deal with Paths when writing a PowerShell Cmdlet?

点点圈 提交于 2019-12-02 14:17:53
What is the proper way to receive a file as a parameter when writing a C# cmdlet? So far I just have a property LiteralPath (aligning with their parameter naming convention) that is a string. This is a problem because you just get whatever is typed into the console; which could be the full path or could be a relative path. Using Path.GetFullPath(string) doesn't work. It thinks I'm currently at ~, I'm not. Same problem occurs if I change the property from a string to a FileInfo. EDIT: For anyone interested, this workaround is working for me: SessionState ss = new SessionState(); Directory

Powershell Script to count .txt files only in a directory

我们两清 提交于 2019-12-02 09:07:15
My goal is to send email to some users to notify about the current count of the files inside some folder directories. I need to count files only with .txt extensions and exclude folders and files inside this. please see below illustration U:\TESTING\Main Folder U:\TESTING\Main Folder\Sub Folder 1 U:\TESTING\Main Folder\Sub Folder 2 output result should look somehting like this Main Folder row should be 1 only, but it also includes the sub folders 1 and 2 and the txt files inside those. Here's the part of the code that counts the total files $total = Get-ChildItem $path -Recurse -File -Include

How does one securely handle passwords in a custom written PowerShell cmdlet?

最后都变了- 提交于 2019-12-02 06:25:56
问题 Assume I have a custom PowerShell Cmdlet that exports data and encrypts it using a password. [Cmdlet(VerbsData.Export, "SampleData")] public class ExportSampleData : PSCmdlet { [Parameter(Mandatory = true)] public string Password { get; set; } /* additional parameters */ } How does one appropriately handle the passwords securely? For example, I'd like to prevent the value from being displayed when the administrator types it in the console. Other options include reading a file that contains an

How does one securely handle passwords in a custom written PowerShell cmdlet?

巧了我就是萌 提交于 2019-12-02 00:34:44
Assume I have a custom PowerShell Cmdlet that exports data and encrypts it using a password. [Cmdlet(VerbsData.Export, "SampleData")] public class ExportSampleData : PSCmdlet { [Parameter(Mandatory = true)] public string Password { get; set; } /* additional parameters */ } How does one appropriately handle the passwords securely? For example, I'd like to prevent the value from being displayed when the administrator types it in the console. Other options include reading a file that contains an encrypted password. I'm aware of PSCredential but that requires a user name which makes no sense in

Start-Job including custom cmdlet terminates with strange error

风格不统一 提交于 2019-12-01 22:22:12
I developed some custom cmdlets that serve for different importing tasks to a SharePoint system. Currently all those cmdlets are being run in a serial kind in a single PowerShell script. I want to change this so that each cmdlet gets executed in a separate task (job). The main script starts a new job with Start-Job relating to a separate script that contains the call to the cmdlet. The script starts and executes the cmdlet. I also debugged the code of the cmdlet that gets executed. So far so fine. But after around 15-20 seconds the job just gets terminated with the following error message:

PowerShell cmdlet parameter validation

佐手、 提交于 2019-12-01 18:37:52
I'm writing a custom PowerShell cmdlet, and I would like to know which is the proper way to validate a parameter. I thought that this could be done either in the property set accessor or during Cmdlet execution: [Cmdlet(VerbsCommon.Add,"X")] public class AddX : Cmdlet { private string _name; [Parameter( Mandatory=false, HelpMessage="The name of the X")] public string name { get {return _name;} set { // Should the parameter be validated in the set accessor? if (_name.Contains(" ")) { // call ThrowTerminatingError } _name = value; } } protected override void ProcessRecord() { // or in the