cmdlet

get a folder path from the explorer menu to a powershell variable

守給你的承諾、 提交于 2019-12-01 18:45:56
is it possible to open a explorer window from powershell and store the path selected in the explorer, to a variable? to open explorer window from powershell PS C:> explorer Maybe this script is what you want: Function Select-FolderDialog { param([string]$Description="Select Folder",[string]$RootFolder="Desktop") [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $objForm = New-Object System.Windows.Forms.FolderBrowserDialog $objForm.Rootfolder = $RootFolder $objForm.Description = $Description $Show = $objForm.ShowDialog() If ($Show -eq "OK") { Return $objForm

Set Value of Nested Object Property by Name in PowerShell

青春壹個敷衍的年華 提交于 2019-11-29 16:11:51
I want to set value of nested object property using PowerShell. When you are trying to set the value of the first level properties, it's quiet simple: $propertyName = "someProperty" $obj.$propertyName = "someValue" # ← It works For nested properties, it doesn't work: $propertyName = "someProperty.someNestedProperty" $obj.$propertyName = "someValue" # ← It doesn't work and raises an error. How to set value of nested object property by name of property using PowerShell? MCVE For those who want to reproduce the problem, here is a simple example: $Obj= ConvertFrom-Json '{ "A": "x", "B": {"C": "y"}

Hosted PowerShell cannot see Cmdlets in the same Assembly

坚强是说给别人听的谎言 提交于 2019-11-29 06:44:07
I'm trying to run PowerShell scripts from my C# code, that will use custom Cmdlets from the assembly that runs them. Here is the code: using System; using System.Management.Automation; [Cmdlet(VerbsCommon.Get,"Hello")] public class GetHelloCommand:Cmdlet { protected override void EndProcessing() { WriteObject("Hello",true); } } class MainClass { public static void Main(string[] args) { PowerShell powerShell=PowerShell.Create(); powerShell.AddCommand("Get-Hello"); foreach(string str in powerShell.AddCommand("Out-String").Invoke<string>()) Console.WriteLine(str); } } When I try to run it, I get

PowerShell - How do I call a cmdlet in a function when overriding that cmdlet's name with the same name as the function?

℡╲_俬逩灬. 提交于 2019-11-29 03:03:26
问题 So I have a cmdlet named update-name that I have no access to change. I have created a function named update-name (the same name as the cmdlet). How do I call the cmdlet from the function with the same name? I've tried a few things and none of them seem to work. function update-name { param([string] something) #call cmdlet update-name here } There is a way to do it when it is just functions: $unBackup = 'DefaultUpdateName' if(!(Test-Path Function:\$unBackup)) { Rename-Item Function:\Update

How to properly use the -verbose and -debug parameters in custom cmdlet

南笙酒味 提交于 2019-11-28 17:27:11
By default, any named function that has the [CmdletBinding()] attribute accepts -debug and -verbose (and a few others) parameters and has predefined $debug and $verbose variables. What I'm trying to figure out is how to pass them on to other cmdlet's that that get called within the function. Let's say I have a cmdlet like this: function DoStuff() { [CmdletBinding()] PROCESS { new-item Test -type Directory } } if -debug or -verbose was passed into my function I want to pass that flag into the new-item cmdlet. What's the right pattern for doing this? Roman Kuzmin Perhaps it sounds strange but

Set Value of Nested Object Property by Name in PowerShell

£可爱£侵袭症+ 提交于 2019-11-28 08:57:38
问题 I want to set value of nested object property using PowerShell. When you are trying to set the value of the first level properties, it's quiet simple: $propertyName = "someProperty" $obj.$propertyName = "someValue" # ← It works For nested properties, it doesn't work: $propertyName = "someProperty.someNestedProperty" $obj.$propertyName = "someValue" # ← It doesn't work and raises an error. How to set value of nested object property by name of property using PowerShell? MCVE For those who want

Hosted PowerShell cannot see Cmdlets in the same Assembly

☆樱花仙子☆ 提交于 2019-11-28 00:09:15
问题 I'm trying to run PowerShell scripts from my C# code, that will use custom Cmdlets from the assembly that runs them. Here is the code: using System; using System.Management.Automation; [Cmdlet(VerbsCommon.Get,"Hello")] public class GetHelloCommand:Cmdlet { protected override void EndProcessing() { WriteObject("Hello",true); } } class MainClass { public static void Main(string[] args) { PowerShell powerShell=PowerShell.Create(); powerShell.AddCommand("Get-Hello"); foreach(string str in

How to properly use the -verbose and -debug parameters in a custom cmdlet

主宰稳场 提交于 2019-11-27 10:15:54
问题 By default, any named function that has the [CmdletBinding()] attribute accepts the -debug and -verbose (and a few others) parameters and has the predefined $debug and $verbose variables. I'm trying to figure out how to pass them on to other cmdlet's that get called within the function. Let's say I have a cmdlet like this: function DoStuff() { [CmdletBinding()] PROCESS { new-item Test -type Directory } } If -debug or -verbose was passed into my function, I want to pass that flag into the new

How to get the current directory of the cmdlet being executed

安稳与你 提交于 2019-11-27 06:16:54
This should be a simple task but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance when I execute c:\temp\myscripts\mycmdlet.ps1 which has a settings file at c:\temp\myscripts\settings.xml I would like to be able to store c:\temp\myscripts in a variable within mycmdlet.ps1 . This is one solution which works (although a bit cumbersome): $invocation = (Get-Variable MyInvocation).Value $directorypath = Split-Path $invocation.MyCommand.Path $settingspath = $directorypath + '\settings.xml' Another one suggested

How to get the current directory of the cmdlet being executed

纵饮孤独 提交于 2019-11-26 10:16:08
问题 This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute C:\\temp\\myscripts\\mycmdlet.ps1 which has a settings file at C:\\temp\\myscripts\\settings.xml I would like to be able to store C:\\temp\\myscripts in a variable within mycmdlet.ps1 . This is one solution which works (although a bit cumbersome): $invocation = (Get-Variable MyInvocation).Value