The following script runs fine, but when executed using UiPath, doesn’t run, any help is appreciated. The domain, useraccount and password is hardcoded for now, but will be extracted from a library for Cyberark later. The servers.txt file holds all the servers from which windows event log entries is extracted for certain events in the past 24 hours.
#Initialize
$OutputFilePath=‘C:\Users\admin\Desktop\evtlogerrors.txt’
$domain = ‘Domain’
$account = ‘UserAccount’
$pwdplaintext = “Password”
$adminaccount = $domain + "" + $account
write-host “Domain is $domain, account is $account, password is $pwdplaintext”
generate secure credentials
$encryptedpassword = $($pwdplaintext |ConvertTo-SecureString -AsPlainText -Force) | ConvertFrom-SecureString
$credential = $encryptedpassword |Convertto-SecureString
$UNPASSWORD = New-Object System.Management.Automation.PsCredential $adminaccount, $credential
#Save the current Trusted hosts value
$trustedHostsList = Get-Item WSMan:\localhost\Client\TrustedHosts;
Read a list of computers from a text file
Get-Content “C:\Powershell\Servers.txt” | ForEach-Object {
This is the computer name, a line from the text file: $_
$Computer = $_
#Add computer to the Trusted hosts list
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $computer -Force;
#create new session
$session = New-PSSession -ComputerName $computer -Credential $UNPASSWORD;
#Invoke-command using the session
$Evtlog1 = Invoke-Command -Session $session -ScriptBlock{Get-EventLog -After (Get-Date).AddDays(-1) -LogName Application | Where-Object {$_.EventID -in ‘18264’,‘3014’,‘14421’,‘102’,‘208’,‘14265’,‘17147’,‘17148’,‘17311’,‘5084’,‘13604’,‘45201’}} | out-file -FilePath $OutputFilePath -append;
$Evtlog1
#$Computer
#End the session
Remove-PSSession $session;
Start-sleep 5
}
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $trustedHostsList.Value -Force;