I’ve been developing an computer asset info script which is run on the local computer to collect WMI/CIMInstance information. I’m at the next stage attempting to apply to remote computer(s).
Some of the things looking for are
Check Connection to computer - Best scripting method?
Check if WinRM/PSRemoting is running as this is needed for CIM
I will need to make sure running PSVersion 3 and above
Thanks for the suggestions I’ll show the code I’ve been working on to explain what I’m trying to achieve.
#Initialize
Write-Output "Initialising"
#---------------------------------------------------------------------
# Process each ComputerName
#---------------------------------------------------------------------
Write-Verbose "=====> Processing $ComputerName <====="
$htmlreport = @()
# Request or Support Number
# Used to pull correct date format for Australia
$Date = Get-Date -Format "dd.MM.yyyy"
# Name of HTML file to exported. Save to same path as script is run from.
$htmlfile = "$PSScriptRoot\$Computername-SystemHTMLReport-$date.html"
# Define Parameters for when running script locally
$Local = @("127.0.0.1","localhost",".","$($env:COMPUTERNAME)")
#---------------------------------------------------------------------
# Make sure WinRM has been started for CIMInstance and other queries to work properly.
#---------------------------------------------------------------------
# Determine if localAddress is being used or remote
if($LocalAddress -contains $ComputerName){$CIMSession = 'false'} else {$CIMSession = 'true'}
# Make sure WinRM has been started for CIMInstance and other queries to work properly.
if($CIMSession){
if(Get-Service WinRM | Where-Object {$_.Status -eq 'Stopped'}){
Write-Warning "WinRM is not running. Please enable WinRM before running script or run Enable-PSRemoting";
start-sleep 3;
stop
} else {
Write-Verbose "WinRM is running!"}
}
#---------------------------------------------------------------------
# Collect Dashboard information and convert to HTML fragment
#---------------------------------------------------------------------
$SystemInfo = {
Write-Output "*Collecting System Information"
# CIM Parameters without session (remote). ScriptBlock will work out to use
$cs = Get-CimInstance -ClassName win32_computersystem
$os = Get-CimInstance -ClassName Win32_operatingsystem
$bs = Get-CimInstance -ClassName win32_bios
$bb = Get-CimInstance -ClassName Win32_BaseBoard
$wv = Get-CimInstance -ClassName Win32_Volume
$vc = Get-CimInstance -ClassName Win32_VideoController
$cpu = Get-CimInstance -ClassName Win32_Processor
$power = Get-CimInstance -Namespace root\cimv2\power -Class win32_PowerPlan
try {
#Get CIM objects based on Connection
#These variables are passed through the whole script so can be used over and over.
$compinfo = @{'Name' = $cs.Name
'Bios Serial Number' = $bs.SerialNumber
'Operating System' = $os.Caption
'Last Boot' = $os.LastBootUpTime
'Domain Connected' = $cs.PartOfDomain
'OS Architecture' = $os.OSArchitecture}
$obj = New-Object -TypeName PSObject -Property $compinfo
Write-Output $obj
$SystemInfoHTML = $obj | ConvertTo-Html -Fragment -As List -PreContent "<h2>System Information</h2>"
}
catch
{
Write-Warning $_.Exception.Message
$SystemInfoHTML = ConvertTo-Html -Fragment -PreContent "<p>An error was encountered. $($_.Exception.Message)</p>"
}
}
#Run Scriptblock locally or remotely
if($CIMSession -eq 'True') {
$Session = New-PSSession -ComputerName $ComputerName
Invoke-Command -Session $Session -ScriptBlock $SystemInfo |
Get-PSSession | Remove-PSSession}
else {Invoke-Command -ScriptBlock $SystemInfo}
Question is the code I’ve got efficient to do what I’m asking using Invoke-Command?
I’ve found using this code works fine from workgroup to workgroup computers or domain to domain but not workgroup to domain…What do I need to do to adjust?
So what is the problem wheny ou use this code for this particular case? You will have to provide credentials to be able to access the traget computers.
Either add creds to your script, or create a domain account with the same username and password as the user running the script in workgroup. Either one would need admin rights.