我編寫了PowerShell腳本來更改遠程VM上的服務的登錄用戶。它在我執行它時起作用。然而,當我把它發送給我的同事,腳本看來,其運行沒有任何錯誤,他們檢查了它仍被列爲「本地帳戶」。使用PowerShell腳本更改Windows服務
$account = Read-Host "Please admin account Name"
$password = Read-Host -AsSecureString "Please enter your password"
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$service = Read-Host "Enter service name"
$computers = Get-Content -Path ".\servers.txt"
foreach ($computer in $computers) {
$svc = gwmi Win32_Service -ComputerName $computer -Filter "name='$service'"
$svc.StopService()
$svc.Change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null)
$svc.StartService()
}
Write-Host $service "is now running as" $account
Read-Host
這將是非常有用的知道錯誤是什麼。一個好的起點,你確定他們有一個名爲servers.txt的文本文件與你的代碼在同一個目錄中嗎?他們的PowerShell執行策略是否允許遠程代碼運行?此外,你讓他們輸入他們的密碼。您應該使用'Get-Credential',否則會更安全。 – FoxDeploy
我輸出'$ computer',因爲聽起來你的腳本沒有找到'。\ servers.txt'並且認爲它沒有任何關係。 – Filburt
for server.txt正在從它們更新的servers.txt文件讀取VM的名稱。沒有提供任何錯誤消息,它看起來好像沒有問題,但沒有對其指定的虛擬機進行更改。 –