如何在没有密码的PSCredential中创建实例?
我试过的事情:
Get-Credential
错误:无法处理参数,因为参数"password"的值为null
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $null)
错误:ConvertTo-SecureString:无法将参数绑定到参数'String',因为它为null.
$mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force))
错误:ConvertTo-SecureString:无法将参数绑定到参数'String',因为它是一个空字符串.
我已经构建了一个小的PowerShell脚本来检查服务是否已启动.如果没有启动,请尝试启动它,然后等待一分钟再次检查.继续重复此过程,直到服务成功启动.我发现循环不像我预期的那样,因为我似乎必须在循环中重新分配服务变量才能获得更新状态.这是我的代码:
$ServiceName = 'Serenade'
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne 'Running'){
$ServiceStarted = $false}
Else{$ServiceStarted = $true}
while ($ServiceStarted -ne $true){
Start-Service $ServiceName
write-host $arrService.status
write-host 'Service started'
Start-Sleep -seconds 60
$arrService = Get-Service -Name $ServiceName #Why is this line needed?
if ($arrService.Status -eq 'Running'){
$ServiceStarted = $true}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行代码没有第三行(带注释的那一行),我得到以下输出.我可以检查Windows服务管理器,并在第一次循环后显然启动了服务.为什么需要第三个最后一行?
鉴于此行为,是否有更好的方法来编写此代码?
谢谢