Forum Discussion
powershell: how to authenticate workgroups PCs by script
Hi all,
I have a little network, few PCs in a workgroup, windows 10 and 11 pro.
I have written a .ps1 script to access remotely PCs and check several configs.
It happens that, if one PC is rebooted, the script doesnt work fine since it is not authenticated.
If I navigate to the PC it asks me the credentials, I put in them successfully (by hand), and then the scritp runs fine.
Is there a way to write this authentication into my script?
this is my script beginning, as first I ping the PC to know if it is online, then I access it, and I thought it was enaugh, but it isnt:
# 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
# open remote sessions
foreach ($Computer in $ComputerName)
{
#Parameters for Credentials
$UserName = "Administrator"
$PassWord = ConvertTo-SecureString -String $Computer -AsPlainText -Force
Write-Output "accessing $Computer..."
#Create a Credential object
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName, $PassWord
Get-WMIObject Win32_Service -computer $Computer -Credential $Cred
#Get-WinEvent -computername $Computer -Credential $Cred
#$session = new-pssession -computername $Computer -Credential $Cred
Write-Output "access to $Computer done..."
}
thanks!
1 Reply
- Varun_GhildiyalIron Contributor
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 $CredentialWrite-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.