2016-09-22 9 views
0

powershellを使用してリモートバッチスクリプトを呼び出そうとしています。ここで特定の資格情報を使用してリモートコマンド/スクリプトを呼び出す

は、いくつかのサンプルコードです:

Invoke-Command -ComputerName 127.0.0.1 {C:\myfile.bat} 

myfile.batはこれを含んでいます

echo %USERNAME% 
copy \\10.10.10.10\sample c:\toMe 

私はCMDウィンドウでローカルmyFile.batを呼び出すと、すべてが正常です。私のユーザー名が必要です。私はPowerShellを使用して、Invoke-Commandコマンドを実行すると、ユーザー名が正しいている間しかし、それは、私は、このエラーを与える:ここで

Access is denied. 

は、私がこれまで試してみましたものです。

$password = ConvertTo-SecureString "MyPassword" -AsPlainText -Force 
$username = "Domain\Username" 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password 
Invoke-Command -ComputerName 127.0.0.1 {myfile.bat} -credential $cred 

これは、同じユーザ名に相当しながら、しかし、それはまだ私に同じエラーを与えます。

私はその後PSDriveコマンドを作成し、同様のコマンドを実行しようとした:

New-PSDrive -Name X -PSProvider FileSystem -Root \\10.10.10.10\sample 
Invoke-Command -ComputerName 127.0.0.1 {myfile.bat} 
Remove-PSDrive X 

しかし、私も同じエラーを取得します。私は今完全に困惑している。バッチスクリプトを変更せずに、この要求を満たすためにPowerShellで何ができますか?

答えて

0

これはあなたのために機能しますか?

$password='[email protected]'|convertto-securestring -asplaintext -force; 
$cred=new-object -typename system.management.automation.pscredential('Domain\Username',$password); 
Invoke-Command -computer 127.0.0.1 {c:\myfile.bat} -credential $cred; 

または

$password='[email protected]'|convertto-securestring -asplaintext -force; 
$cred=new-object -typename system.management.automation.pscredential('Domain\Username',$password); 
$s = New-PSSession -computer "127.0.0.1" -credential $cred; 
Invoke-Command -Session $s -ScriptBlock { cmd /c "c:\myfile.bat" }; 
Remove-PSSession $s; 

また、あなたはusername

domain\usernameを変更することがありテスト
関連する問題