How to execute powershell script in remote servers

I have a script to check validation of images and the script works fine when I locally run on each image. But when I ran the below script via automation tool " Jenkins"it is not running on remote machine, behalf of that it runs on same machine where jenkins tool installed. 

Can any one let me know is there any change need to be done on the script when I try running this script on a remote server ?

 

$reportpath = ".\Report.htm" 

if((test-path $reportpath) -like $false)
{
new-item $reportpath -type file
}

###############################HTml Report Content############################
$report = $reportpath

Clear-Content $report 
Add-Content $report "<html>" 
Add-Content $report "<head>" 
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>" 
Add-Content $report '<title>AD Status Report</title>' 
add-content $report '<STYLE TYPE="text/css">' 
add-content $report  "<!--" 
add-content $report  "td {" 
add-content $report  "font-family: Calibri;" 
add-content $report  "font-size: 13px;" 
add-content $report  "border-top: 0px solid #999999;" 
add-content $report  "border-right: 0px solid #999999;" 
add-content $report  "border-bottom: 0px solid #999999;" 
add-content $report  "border-left: 0px solid #999999;" 
add-content $report  "padding-top: 0px;" 
add-content $report  "padding-right: 0px;" 
add-content $report  "padding-bottom: 0px;" 
add-content $report  "padding-left: 5px;" 
add-content $report  "}" 
add-content $report  "building {" 
add-content $report  "margin-left: 5px;" 
add-content $report  "margin-top: 5px;" 
add-content $report  "margin-right: 0px;" 
add-content $report  "margin-bottom: 10px;" 
add-content $report  "" 
add-content $report  "table {" 
add-content $report  "border: thin solid #000000;" 
add-content $report  "}" 
add-content $report  "-->" 
add-content $report  "</style>" 
Add-Content $report "</head>" 
Add-Content $report "<building>" 
add-content $report  "<table width='40%'>" 
add-content $report  "<tr bgcolor='blue'>" 
add-content $report  "<td colspan='7' height='40' align='center'>" 
add-content $report  "<font face='Calibri' color='white' size='4'><strong>Windows Image Validation Report</strong></font>" 
add-content $report  "</td>" 
add-content $report  "</tr>" 
add-content $report  "</table>" 
 
add-content $report  "<table width='40%'>" 
Add-Content $report  "<tr bgcolor='#0B0B3B' height='30'>" 

Add-Content $report  "<td width='2%' align='center'><B><font face='Calibri' color='white' size='4'><strong>SL No.</strong></font></B></td>"
Add-Content $report  "<td width='7%' align='center'><B><font face='Calibri' color='white' size='4'><strong>Item</strong></font></B></td>" 
Add-Content $report  "<td width='9%' align='center'><B><font face='Calibri' color='white' size='4'><strong>Result</strong></font></B></td>"

Add-Content $report "</tr>" 


$ComputerName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>1</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B> Computername</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$ComputerName</B></td>"
Add-Content $report "</tr>"

#Image Name & Image Share===================================================================================================================================

if (Test-Path "C:\Windows\ImageLog.ini")
{
$Image1 = Get-Content C:\Windows\ImageLog.ini | Select-String -Pattern "Image Name:"; $Image2 = $Image1.ToString();$ImageName = $Image2.Split(":")[1].Trim()
    if ($ImageName -eq $null)
    {$Image1 = Get-Content C:\Windows\ImageLog.ini | Select-String -Pattern "Appstack Name:"; $Image2 = $Image1.ToString();$ImageName = $Image2.Split(":")[1].Trim()}
$Share1 = Get-Content C:\Windows\ImageLog.ini | Select-String -Pattern "ImageShare:"; $Share2 = $Share1.ToString();$ShareName = $Share2.Split(":")[1].Trim()
}

if (Test-Path "c:\WinPEDeploy.log")
{
$Image1 = Get-Content c:\WinPEDeploy.log | Select-String -Pattern "Selected Image:"; $Image2 = $Image1.ToString();$ImageName = $Image2.Split(":")[1].Trim()
$Share1 = Get-Content c:\WinPEDeploy.log | Select-String -Pattern "Image Server:"; $Share2 = $Share1.ToString();$ShareName = $Share2.Split(":")[1].Trim()
}




#Image Name ================================================================================================================================================

if ($ImageName -eq $null)
{
$ImageName = "Unavailable"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>2</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B> ImageName</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$ImageName</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>2</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B> ImageName</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$ImageName</B></td>"
Add-Content $report "</tr>"
}

#Image Share==============================================================================================================================================

if ($ShareName -eq $null)
{
$ShareName = "Unavailable"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>3</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Image Server Used</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$ShareName</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>3</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Image Server Used</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$ShareName</B></td>"
Add-Content $report "</tr>"
}

#KMS Server Name===========================================================================================================================================

$KMS = reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform" /v "KeyManagementServiceName"| select-String -Pattern "KeyManagementServiceName" | Out-String
$KMSServer = $KMS.trim().Split("")[8]

if ($KMSServer -eq $null)
{
$KMSServer = "Unavailable"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>4</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>KMS Server</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$KMSServer</B></td>"
Add-Content $report "</tr>"
}
else
{

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>4</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>KMS Server</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$KMSServer</B></td>"
Add-Content $report "</tr>"
}

#Windows Activation Status========================================================================================================================================

$License = "cscript c:\windows\system32\slmgr.vbs /dli | find /I " + '"License Status:"'
$LIC = Invoke-Expression ($License) ; $Activated = $LIC.Split(":")[1].trim()

if ($Activated -eq $null)
{
$Activated = "Unavailable"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>5</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>OS Activation Status</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$Activated</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>5</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>OS Activation Status</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$Activated</B></td>"
Add-Content $report "</tr>"
}

#Part Of Domain===================================================================================================================================================

if ((gwmi win32_computersystem).partofdomain -eq $true) 
{
$DomainMember = "Yes" 
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>6</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Is Part of Domain</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$DomainMember</B></td>"
Add-Content $report "</tr>"


else 
{
$DomainMember = "NO"
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>6</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Is Part of Domain</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$DomainMember</B></td>"
Add-Content $report "</tr>"
}

#Domain Name===================================================================================================================================================

$DomainName = (gwmi win32_computersystem).domain
if ($DomainName -eq "oim.oimcorp.com")
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>7</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Domain Name</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$DomainName</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>7</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Domain Name</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$DomainName</B></td>"
Add-Content $report "</tr>"
}

#McAfee Service==============================================================================================================================================

$McAfee = get-wmiobject win32_service | where-object {$_.Name -eq 'McAfeeFramework'} | format-list state | Out-String
$McAfeeStatus = $McAfee.Split(":")[1].Trim()

if ($McAfeeStatus -eq "Running")
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>9</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>McAfee Service Status</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$McAfeeStatus</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>9</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>McAfee Service Status</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$McAfeeStatus</B></td>"
Add-Content $report "</tr>"
}

#ePO Server Name============================================================================================================================================

if (Test-Path ("HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent"))
{$ePoReg = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent"} else {$ePoReg = "HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent"}

$ePO = reg query $ePoReg /v "ePOServerList" | Select-String -Pattern "ePOServerList" ; $ePO1 = $ePO.ToString().Trim()
$ePOServer = $ePO1.Split("")[8].Split("|")[0]


if ($ePOServer -eq "slcap138.us.oim.com")
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>10</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>McAfee ePO Server</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$ePOServer</B></td>"
Add-Content $report "</tr>"
}
if (($ePOServer -ne "slcap138.us.oim.com") -and ($ePOServer -ne $null))
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>10</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>McAfee ePO Server</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$ePOServer</B></td>"
Add-Content $report "</tr>"
}
if ($ePOServer -eq $null)
{
$ePOServer = "Unavailable"
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>10</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>McAfee ePO Server</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$ePOServer</B></td>"
Add-Content $report "</tr>"
}

#UAC Status==================================================================================================================================================

$UAC = reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v "EnableLUA" | select-String -Pattern "EnableLUA" | Out-String
$UAC1 = $UAC.Split(" ")[12].Trim()

if ($UAC1 -eq $null)
{
$UACStatus = "Unavailable"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>13</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>UAC Status</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$UACStatus</B></td>"
Add-Content $report "</tr>"

}
elseif ($UAC1 -eq "0x0")
{
$UACStatus = "UAC Disabled"
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>13</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>UAC Status</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$UACStatus</B></td>"
Add-Content $report "</tr>"
}

elseif ($UAC1 -eq "0x1")
{
$UACStatus = "UAC Enabled"
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>13</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>UAC Status</B></td>"
    Add-Content $report "<td bgcolor= 'Yellow'  height='30' align=left><B>$UACStatus</B></td>"
Add-Content $report "</tr>"
}

#Driver Check===================================================================================================================================================

$PV = Get-WmiObject Win32_PnPSignedDriver| select devicename, driverversion | where {$_.devicename -like "*oim VM Virtual PCI Bus*"} | select-string -pattern "driver" | select -last 1 | Out-String

$PVVersion = ""
$PVName = ""


$PVVersion = $PV.split(";")[1].split("=")[1].split("}")[0].Trim()
$PVName = $PV.split(";")[0].split("=")[1].Trim()

echo $PVName
echo $PVVersion

If (($PVName -eq $null) -and ($PVVersion -eq $null))
{
$PVStat = "Unavailable. May not be OVM guest"


Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>14</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>PV Driver Type & Version</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$PVStat</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>14</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>PV Driver Type & Version</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$PVName $PVVersion</B></td>"
Add-Content $report "</tr>"
}

#Internet Explorer============================================================================================================================

$IE = reg query "HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer" /v "version" | select-String -Pattern "version" | Out-String
$IEVersion = $IE.trim().Split("")[8]

if ($IEVersion -eq $null)
{
$IEVersion = "Unavailable!!"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>15</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>IE Version</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$IEVersion</B></td>"
Add-Content $report "</tr>"
}

elseif ($IEVersion -like "*11*")
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>15</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>IE Version</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$IEVersion</B></td>"
Add-Content $report "</tr>"
}

elseif ($IEVersion -ne "*11*")
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>15</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>IE Version</B></td>"
    Add-Content $report "<td bgcolor= 'Yellow'  height='30' align=left><B>$IEVersion</B></td>"
Add-Content $report "</tr>"
}

$proxies = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyServer

if ($proxies -eq $null)
{
$proxies = "Unavailable"

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>16</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Proxy Server</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$proxies</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>16</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>Proxy Server</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$proxies</B></td>"
Add-Content $report "</tr>"
}
    
# Table Close =================================================================================================================================

Add-content $report  "</table>" 
Add-Content $report "</building>" 
Add-Content $report "</html>" 

# Email Result =================================================================================================================================

$smtphost = "internal-mail.oim.com" 
$from = "Image.Validation@oim.com" 
$email1 = "test_org_ww@oim.com" 
$timeout = "60"

$subject = "Imaging Validation Report For Host - $ComputerName" 
$building = Get-Content ".\Report.htm" 
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
$msg = New-Object System.Net.Mail.MailMessage 
$msg.To.Add($email1)
$msg.from = $from
$msg.subject = $subject
$msg.building = $building 
$msg.isBodyhtml = $true
$smtp.send($msg)

  • I have no idea what this 'Jenkins' automation tool is and never heard of it until now, but a quick web search shows is a real thing and integrates with HP Application Automation Tools.

    Nonetheless..., this tool notwithstanding... running commands and scripts on remote machines is well documented and provided using PowerShell Remoting.

    PowerShell Remoting has to be enabled on the hosts that you plan to communicate with and you must have permissions to use PowerShell Remoting as well as permission on any host you are connecting to.

    When it comes to managing remote computers with PowerShell, you have essentially three options.
    You can open an interactive session with the Enter-PSSession cmdlet (One-to-One Remoting).
    An alternative is the Invoke-Command cmdlet, which allows you to run remote commands on multiple computers (which is why it is called One-to-Many Remoting).
    The third option is to use one of those cmdlets that offer a ComputerName parameter.
    In most cases, PowerShell Remoting isn’t involved then

    Get-Command -Name *remoting*

    CommandType Name Version Source
    ----------- ---- ------- ------

    Cmdlet Disable-PSRemoting 3.0.0.0 Microsoft.PowerShell.Core
    Cmdlet Enable-PSRemoting 3.0.0.0 Microsoft.PowerShell.Core

    Get-Help -Name Enable-PSRemoting -Full

    'msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/enable-psremoting'


    Get-Help -Name Invoke-Command -Full
    'msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/invoke-command'


    Remotely execute cmd.exe commands on multiple computers
    'gallery.technet.microsoft.com/scriptcenter/56962f03-0243-4c83-8cdd-88c37898ccc4'


    A web search using these titles are eBooks that takes you through the ins and outs of this Remoting effort.
    'Secrets of PowerShell Remoting'
    ' Layman’s guide to PowerShell 2.0 remoting'

    BTW, you could have done the run on remote computers well before PowerShell as well using VBScript and WMI.
    'technet.microsoft.com/en-us/library/ee692838.aspx'
  • In reply to postanote:

    Thanks for the reply.

    I was trying to execute my above powershell script which is copied in C:\ drive of the server to execute in 3 remote server which is mentioed in server.txt file in C:\ , But it throw an error message like below.

    Can you please help me here.

    PS C:\Users\smenon\Desktop> Invoke-Command -ScriptBlock {Get-Item C:\ValidationInvoke.ps1} -ComputerName (Get-Content C:\server.txt)

    Directory: C:\


    Mode LastWriteTime Length Name PSComputerName
    ---- ------------- ------ ---- --------------
    -a--- 2/2/2017 11:42 AM 31795 ValidationInvoke.ps1 kkm00bue
    Cannot find path 'C:\ValidationInvoke.ps1' because it does not exist.
    + CategoryInfo : ObjectNotFound: (C:\ValidationInvoke.ps1:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

    Cannot find path 'C:\ValidationInvoke.ps1' because it does not exist.
    + CategoryInfo : ObjectNotFound: (C:\ValidationInvoke.ps1:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
  • In reply to smenon:

    OK, so, let me get my head around this.

    Stick with me, as I have OPP (old people problems) 8^) and you know, sometimes, we can get confused and require more edification. 8^)

    You created the above script and copied that script to the Jenkins server C:\drive ?

    Then you are trying to use PSRemoting to the Jenkins server from your workstation, to run the script there which in turn uses PSRemoting to a list of other servers, using resources located on the Jenkins server?

    So, in essence, you are trying to use your workstation remote session to trigger a script that triggers another server remote session to a remote set of servers via your script?

    That's a bit of a head scratcher... 8^)
    And something I need to play around with.

    This is like RDP to one server to RDP to another, to RDP to other servers, which you can do, both those are independent actions.

    As documented, PSRemoting is a one-to-one or one-to-many thing. Not a one-to-one-to-many.

    You need to run the script from the source (Jenkins) server directly, to the destination server(s).

    So, this means either you RDP to the Jenkins Server and execute the script for the remote servers from there, or using a scheduled task on the Jenkins server with an account that has the permissions on the other servers for the remote session. Of course you can dynamically create that scheduled task via PoSH as well.

    Or use Explicit remoting to the Jenkins server to execute the script interactively.

    'blogs.technet.microsoft.com/heyscriptingguy/2012/07/25/an-introduction-to-powershell-remoting-part-three-interactive-and-fan-out-remoting'

    Or use implicit remoting session, then use that session to execute the script.

    'blogs.technet.microsoft.com/heyscriptingguy/2012/07/26/an-introduction-to-powershell-remoting-part-four-sessions-and-implicit-remoting'
  • In reply to postanote:

    Yes, I have a jenkin master and slave configured. I have logged in to jenkins slave and from there I am trying to execute powershell command

    Invoke-Command -filepath C:\ValidationInvoke.ps1} -ComputerName (Get-Content C:\server.txt)

    Note : All remote server has WInrm enabled
  • In reply to smenon:

    The snip you include above, has some syntax issues, but I am sure you are using it correctly on your side since you are getting responses. Nonetheless...

    Here is a simple quick test I did to give you the idea of what you can do for your effort.

    # Servers.txt
    Server1
    Server2
    Node1
    Node2


    Do-SomeThing.ps1
    # Begin Script

    function Do-SomeStuff
    {
    "Calling a local function for $env:COMPUTERNAME --- Whaaa ...Zupppp!!!???"
    whoami
    Get-Date
    }

    "Calling a script for $env:COMPUTERNAME --- Whaaa ...Zupppp!!!???"
    whoami
    Get-Date

    # End Script



    Use-SomeScript.ps1
    # Begin Script

    . .\Do-SomeThing.ps1
    Get-ChildItem -Path Function:\Do-SomeStuff

    $RemoteServers = Get-Content -Path 'C:\Scripts\Servers.txt'
    $RemoteServers | Test-WSMan

    ForEach ($RemoteServerName in $RemoteServers)
    { Invoke-Command -ComputerName $RemoteServerName -FilePath '.\Do-SomeThing.ps1' }


    ForEach ($RemoteServerName in $RemoteServers)
    { Invoke-Command -ComputerName $RemoteServerName -ScriptBlock ${Function:Do-SomeStuff} }

    # End Script



    The results of the above will return results of the local script run on the remote server as well as the function call from the local script ran on the remote server.

    Calling a script for SERVER1 --- Whaaa ...Zupppp!!!???
    Contoso\administrator
    Friday, February 3, 2017 9:32:33 PM
    ...

    Calling a local function for SERVER1 --- Whaaa ...Zupppp!!!???
    Contoso\administrator
    Friday, February 3, 2017 9:32:33 PM
    ...
  • In reply to postanote:

    Thanks alot.

    I have created a script like below.

    Is that possible to add our credential also in this for loop. So that the script use the particular credential to login ?

    ForEach ($Computer in Get-Content C:\server.txt)
    {
    Try
    {
    echo "$Computer"
    Invoke-Command -ComputerName $Computer -FilePath C:\Validation.ps1
    }
    Catch
    {
    Add-Content Unavailable-Computers.txt $Computer


    }
    }
  • In reply to smenon:

    No wories, however, my first question is why the need to use different creds per remote host?

    Domain Admins, are local admins by default and you can create a new AD Security group, add users to that then make that a custom local logon group/or local admins of course, on the target servers, to avoid using such high priv accounts.

    However, as to your query, sure using the credential param of Invoke-Command. You'd have to store the creds in a file and then call as needed. Just be aware of the operational and risk management aspects when doing this and steps to keep it under control.

    The other option, if you can leverage PoSHv5 is using JEA (Just Enough Administration) for this effort.

    Just Enough Administration: Windows PowerShell security controls help protect enterprise data

    'msdn.microsoft.com/en-us/library/dn896648.aspx'

    You can store multiple creds in a single file as shown in another sample on this very site.

    'community.idera.com/search?q=Saving%20Multiple%20Credentials'

    # Saving multiple credentials

    $CredPath = "$home\Desktop\mycreds.xml"
    $creds = @{
    Local = Get-Credential -Message LocalAccount
    Remote = Get-Credential -Message RemoteAccount
    Domain = Get-Credential -Message DomainAccount
    }

    $creds | Export-Clixml -Path $OutPath


    To read in these credentials, use this:

    $CredPath = "$home\Desktop\mycreds.xml"
    $creds = Import-Clixml -Path $CredPath

    $creds.Local
    $creds.Remote
    $creds.Domain

    # example
    Get-WmiObject -Class Win32_BIOS -ComputerName server1 -Credential $creds.Remote


    Note that the passwords are safely encrypted with your identity and the machine identity, so you can read in the credentials only on the machine where they were generated, and only if you are the same person that saved them.
  • In reply to postanote:

    mycreds.xml has any information like userid and password ?
  • In reply to smenon:

    The creds object will contain both. Username is plaintext and password encrypted.

    If you are just after the pieces, then just ask for it, just as you would do inline.
     
     
    $creds = @{
    Local = Get-Credential "$env:COMPUTERNAME\LocalAccount"
    Remote = Get-Credential 'RemoteDomain\RemoteAccount'
    Domain = Get-Credential "$env:UserDOMAIN\DomainAccount"
    }

    $creds


    # Results

    Name Value
    ---- -----
    Local System.Management.Automation.PSCredential
    Domain System.Management.Automation.PSCredential
    Remote System.Management.Automation.PSCredential


    $creds.Local.UserName
    $creds.Local.Password



    # Results

    LabWS\LocalAccount
    System.Security.SecureString