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)

Parents
  • 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'
Reply Children