PowerShell升级远程机器的windows service的脚本(最终版)

$s   =  Get - WmiObject  - computer  10.10 .zz.zz  Win32_Service  - Filter   " Name='XXX' "   - credential  ( Get-Credential XXXXXX\fanwx)
$s .stopservice()
copy
- item D:\.....\aaa.exe  - destination \\ 10.10 . zz.zz \c $ \vvv\
copy
- item D:\.....\aaa.pdb  - destination \\ 10.10 .zz.zz \c $ \vvv\
$s .startservice()

最让人奔溃的是每次执行这段脚本,Get-Credential都会导致弹出要求输入密码的提示框。。。

哪位高手知道如何让这段脚本不再弹?

在windows credential vault里有这个ip的了。

 

不提sc,太慢。。。无法忍受。。。 

-------------------------------------------牛逼的分割线---------------------------------------------------

该问题已解决 :

 $cred = New-Object System.Management.Automation.PsCredential "fanweixiao", (Get-Content c:\my.cred | ConvertTo-SecureString)

$s   =  Get - WmiObject  - computer IPAddress Win32_Service  - Filter   " Name='ServiceName' "   - credential  $cred
if ( $s .state  -eq   " Running " ){
    Write
- Host  " Service is Running, begin to stop it "
    
$s .stopservice()

    
$s   =  Get - WmiObject  - computer IPAddress Win32_Service  - Filter   " Name='ServiceName' "   - credential  $cred
    
while ( $s .state  -ne   " Stopped " ){
        Write
- Host  " Waiting Stop...State= "    $s .state
        Start
- Sleep  - Seconds  1
        
$s   =  Get - WmiObject  - computer IPAddress Win32_Service  - Filter   " Name='ServiceName' "   - credential  $cred
    }
    Write
- Host  " Stopped "
}
copy
- item D:\GitRepo\src\base\ServiceName\ServiceName\bin\Debug\ServiceName.exe  - destination \\IPAddress\c $ \PROJ\ServiceName\
copy
- item D:\GitRepo\src\base\ServiceName\ServiceName\bin\Debug\ServiceName.pdb  - destination \\IPAddress\c $ \PROJ\ServiceName\

$s .startservice()
$s   =  Get - WmiObject  - computer IPAddress Win32_Service  - Filter   " Name='ServiceName' "   - credential  $cred
$s .state

while ( $s .state  -ne   " Running " ){
    Write
- Host  " Waiting Start...Current state is  "   $s .state
    Start
- Sleep  - Seconds  1
    
$s   =  Get - WmiObject  - computer IPAddress Win32_Service  - Filter   " Name='ServiceName' "   - credential  $cred
}
Write
- Host  " Service Started! "

其中,这个cred文件可以这样生成:

$cred = Get-Credential "fanweixiao"  

$cred.Password | ConvertFrom-SecureString | Set-Content c:\my.cred

 感谢薛强, 孟大~

你可能感兴趣的:(PowerShell升级远程机器的windows service的脚本(最终版))