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

 感谢薛强, 孟大~

转载于:https://www.cnblogs.com/fanweixiao/archive/2011/04/13/2014934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值