Powershell登录WORKGROUP计算机

时间:2014-07-11 14:56:18

标签: powershell

我正在创建Powershell脚本以登录WORKGROUP计算机以执行某些操作,但是我得到一个弹出窗口,我手动输入计算机的密码。

$InputFile = 'D:\Script\input.txt'

if(!(Test-Path $InputFile)) {
    Write-Error "File ($InputFile) not found. Script is exiting"
    exit
} 


$Computers = Get-Content -Path $InputFile



$password = Read-Host "Enter the password" -AsSecureString
$confirmpassword = Read-Host "Confirm the password" -AsSecureString
$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($confirmpassword))
if($pwd1_text -ne $pwd2_text) {   
    Write-Error "Entered passwords are not same. Script is exiting"
exit
} 

foreach ($Computer in $Computers) {
    $Computer    =    $Computer.toupper()
    $Isonline    =    "OFFLINE"
    $Status        =    "SUCCESS"    
    Write-Verbose "Working on $Computer"

    if((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {   
        $Isonline = "ONLINE"   
        Write-Output "`t$Computer is Online"
    } 
    else { 
        Write-Output "`t$Computer is OFFLINE" 
    }


    mstsc /v:$Computer


}

如何通过for循环逐个自动登录每台计算机?

请注意,这些WORKGROUP计算机分散在我们的整个组织中,几乎没有任何人工管理。

1 个答案:

答案 0 :(得分:1)

通常密码都附有用户名...不确定您计划使用密码做什么。

$Credentials = get-credential
Get-Content $InputFile | % { 
    New-PSSession -ComputerName $_ -Credential $Credentials -Name $_
}

Get-Help about_Remoting
Get-Help about_PSSessions

相关问题