简单的Powershell脚本在编译或作为脚本运行时不起作用

时间:2013-12-09 14:47:11

标签: powershell powershell-remoting

我有一个简单的代码片段,我可以在powershell控制台中运行没有任何问题。当我将它编译为EXE,甚至是ps1并运行它时,它找不到reg值,不知道为什么。 这是代码:

$User = "Training\Administrator"
$PWord = ConvertTo-SecureString -String "P@ssWord" -AsPlainText -Force 
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord 
$creds = $Credentials 
enter-pssession –computername Win7Client –credential $creds
Start-Sleep -s 2
Set-itemproperty “HKLM:\SOFTWARE\Citrix\Metaframe Password Manager\Extensions\SyncManager\Syncs\DefaultSync\Servers” -name Server1 -value \\DFSI\CPMStore
Return

1 个答案:

答案 0 :(得分:0)

我会将最后一行更改为:

$Pssn = new-psssession –computername Win7Client –credential $creds
invoke-command -Session $Pssn -scriptblock {Set-itemproperty “HKLM:\SOFTWARE\Citrix\Metaframe Password Manager\Extensions\SyncManager\Syncs\DefaultSync\Servers” -name Server1 -value \\DFSI\CPMStore }
Return

希望这有帮助,

吕克