Forum Discussion
powershell: how to authenticate workgroups PCs by script
You can use the New-Object cmdlet to create a PSCredential object and use it in your script to authenticate with the remote computers. Here's an example of how you can modify your script to use the PSCredential object:
# Test ping
workflow Test-Ping
{
param(
[Parameter(Mandatory=$true)]
[string[]]$Computers
)
foreach -parallel -throttlelimit 150 ($Computer in $Computers)
{
if (Test-Connection -Count 1 $Computer -Quiet -ErrorAction SilentlyContinue)
{
$Computer
}
else
{
Write-Warning -Message "$Computer not online"
}
}
}
$ComputerName = Test-Ping -Computers $ComputerName
# Create credential object
$UserName = "username"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($UserName,$Password)
# open remote sessions
foreach ($Computer in $ComputerName)
{
Write-Output "accessing $Computer..."
Get-WMIObject Win32_Service -computer $Computer -Credential $Credential
#Get-WinEvent -computername $Computer -Credential $Credential
#$session = new-pssession -computername $Computer -Credential $Credential
Write-Output "access to $Computer done..."
}
Replace username and password with the actual username and password you use to authenticate with the remote computers.
Using a credential object in your script will allow you to authenticate with the remote computers without being prompted for credentials each time.