Running a script remote

I’m in need of some help running a script remotely to a server.

I need to convert all of my commands to use a parameter called $computername. The name of my script is get-computerstatus.ps1.

I know I can run it like the command below, but I’m stuck with the above.

 

It would help if you provided some code to look at. Have you reviewed Get-Help about_parameters

#Determine Operating System Information and Output only Name,BuildNumber,Version
get-ciminstance -class win32_operatingsystem | format-list Name,BuildNumber,version
#Determine Processor Information and Output only Device ID, Name, MaxClockSpeed
get-ciminstance -class CIM_processor | format-list DeviceID,Name,MaxClockSpeed
#Determine IP Address Configuration
get-ciminstance win32_networkadapterconfiguration | format-table ipaddress,subnetmask,dhcp,defaultgateway
#Acquire DNS Client Server address and output it
Get-DnsClientServerAddress | format-list ServerAddresses
#Determine the amount of system memory in GB
get-ciminstance win32_computersystem | foreach {$_.TotalPhysicalMemory /1GB}
#Determine the amount of of free space (GB) for the c:\ drivey
Get-CimInstance -ClassName cim_logicaldisk | Where-Object {$_.FreeSpace} |
Select-Object @{n='FreeSpace';exp={"{0:N2} GB" -f ($_.FreeSpace / 1GB)}},DeviceID
#Determine last bootup date/time and output computer's name and last boot time
Get-CimInstance win32_operatingsystem | select name,lastbootuptime
#Determine last user login date & time. Output the username and last logon date/time
get-localuser | where-object {$_.Lastlogon} | select-object Name,Lastlogon
#Retrieve all user accounts and output the account name
get-localuser| format-list name
#Determine installed hotfixes & updates. Output only the hotfix ID
get-hotfix | format-list hotfixid
#List all installed applications and output as name,vendor,version
get-ciminstance -class WIn32_product | format-list Name,Vendor,Version

If you must use a computername parameter, here’s what I would do.

param (
    [string[]]$ComputerName
)
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
    #Determine Operating System Information and Output only Name,BuildNumber,Version
    get-ciminstance -class win32_operatingsystem | format-list Name,BuildNumber,version
    #Determine Processor Information and Output only Device ID, Name, MaxClockSpeed
    get-ciminstance -class CIM_processor | format-list DeviceID,Name,MaxClockSpeed
    #Determine IP Address Configuration
    get-ciminstance win32_networkadapterconfiguration | format-table ipaddress,subnetmask,dhcp,defaultgateway
    #Acquire DNS Client Server address and output it
    Get-DnsClientServerAddress | format-list ServerAddresses
    #Determine the amount of system memory in GB
    get-ciminstance win32_computersystem | foreach {$_.TotalPhysicalMemory /1GB}
    #Determine the amount of of free space (GB) for the c:\ drivey
    Get-CimInstance -ClassName cim_logicaldisk | Where-Object {$_.FreeSpace} |
    Select-Object @{n=’FreeSpace’;exp={“{0:N2} GB” -f ($_.FreeSpace / 1GB)}},DeviceID
    #Determine last bootup date/time and output computer’s name and last boot time
    Get-CimInstance win32_operatingsystem | select name,lastbootuptime
    #Determine last user login date & time.  Output the username and last logon date/time
    get-localuser | where-object {$_.Lastlogon} | select-object Name,Lastlogon
    #Retrieve all user accounts and output the account name
    get-localuser| format-list name
    #Determine installed hotfixes & updates.  Output only the hotfix ID
    get-hotfix | format-list hotfixid
    #List all installed applications and output as name,vendor,version
    get-ciminstance -class WIn32_product | format-list Name,Vendor,Version
} #scriptblock

Then you would call your script like this:

.\myscript.ps1 -Computername "svr1", "svr2"

But if you have the flexibility, all you really need to do is use your current script like this:

Invoke-Command -ComputerName "svr1", "svr2" -FilePath ".\currentscript.ps1"