Показаны сообщения с ярлыком powershell. Показать все сообщения
Показаны сообщения с ярлыком powershell. Показать все сообщения

пятница, 24 октября 2008 г.

Create Registry Path in PowerShell


Here is script to create registry path (or separate key) in PowerShell



function vrIsRegistryPathContainsKey($basePath, $key)
{
if($key.Length -le 0)
{ return 0 }

$childItems = Get-ChildItem $basePath -Force -ErrorAction SilentlyContinue
foreach ($childItem in $childItems)
{
if($childItem.name -match $key)
{
return 1
}
}
return 0
}

function vrCreateRegistryPath($basePath, $createdPath)
{
$pathTokens = $createdPath.split("\")
foreach ($pathToken in $pathTokens)
{
if($pathToken.Length -le 0)
{ break }

if(-not(vrIsRegistryPathContainsKey $basePath $pathToken))
{
echo ('not found ' + $pathToken);
New-Item -Path $basePath -Name $pathToken
}
$basePath = $basePath + "\" + $pathToken
echo ('basepath ' + $basePath);
}
}


Usage:
vrCreateRegistryPath 'Microsoft.PowerShell.Core\Registry::\HKEY_LOCAL_MACHINE\SOFTWARE' 'Key\SubKey1\Subkey2\'


вторник, 21 октября 2008 г.

Using .NET Enum values in PowerShell


For using values of enum nested in type and/or namespace of already loaded assembly in PowerShell you would use following statement:


[SomeNamespace.EnclosingType+NestedEnum]::EnumValue


понедельник, 18 августа 2008 г.

How to get PowerShell current runspace from C#

It's possible you faced with situation when you need access to PowerShell current runspace in C# code. For example you need get value of script variable which exists only in runspace of hosting application (i.e. usually poweshell.exe). Of course you can use RunspaceFactory.CreateRunspace(), but the trouble is that new runspace doesn't contain any script variables and added earlier snap-ins.

Here is example how to do this.


Runspace theRunSpace = System.Management.Automation.Runspaces.Runspace.DefaultRunspace;

if (theRunSpace.RunspaceStateInfo.State == RunspaceState.Opened)
{
string theCommand = "$MyScriptVariable";
using (Pipeline thePipeline = theRunSpace.CreateNestedPipeline(theCommand, true))
{
Collection theRetVal = thePipeline.Invoke();
}
}


Note that in shown code CreateNestedPipeline() called instead CreatePipeline(), because at the moment of call CreateNestedPipeline() we stand in executing pipeline already.