Multiple monitors/graphic cards info using powershell - powershell

I am using the following script to get system information. The script works fine, I have problem when computers with two graphic cards or multiple monitors.
For more monitors I found this Get Screen resolution using WMI/powershell in Windows 7
but don't know how to format/implement it to fit my script.
$user = [Environment]::UserName
$System = Get-CimInstance CIM_ComputerSystem
$BIOS = Get-CimInstance CIM_BIOSElement
$OS = Get-CimInstance CIM_OperatingSystem
$CPU = Get-CimInstance CIM_Processor
$osup = Get-WmiObject win32_operatingsystem
$uptime = (Get-Date) - ($osup.ConvertToDateTime($osup.lastbootuptime))
$GPU = Get-CimInstance CIM_VideoController
$RES = Get-WmiObject Win32_VideoController
$used = ($System.TotalPhysicalMemory/1MB)-($OS.FreePhysicalMemory/1KB)
$EXTXT = "$env:USERPROFILE\Desktop\vq.txt"
Clear-Host
$user + "#" + $system.Name >> $EXTXT
"OS: " + $OS.caption + " " + $osup.OSArchitecture >> $EXTXT
"Model: " + $System.Model >> $EXTXT
"Kernel: " + $OS.Version >> $EXTXT
"Uptime: " + $Uptime.Days + "d " + $Uptime.Hours + "h " + $Uptime.Minutes + "m " >> $EXTXT
"Resolution: " + $RES.CurrentHorizontalResolution + "x" + $RES.CurrentVerticalResolution >> $EXTXT
"CPU: " + $CPU.Name >> $EXTXT
"GPU: " + $GPU.Name >> $EXTXT
"Memory: " + "{0:N0}" -f $used + "MB" + " / " + "{0:N0}" -f ($System.TotalPhysicalMemory/1MB) + "MB" >> $EXTXT
I basically want to get file with information in lines, with GPU separated with comma and detection for multiple screens and if there are multiple monitors printed their respective resolutions.

I suggest changing the line $GPU = to
$GPU=(Get-CimInstance CIM_VideoController|%{$_.Name}) -join("; ")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.Screens")
$Screens = ([system.windows.forms.screen]::AllScreens | %{"$($_.Bounds.width)x$($_.Bounds.Height)"}) -join("; ")
and "GPU: " + $GPU.Name >> $EXTXT to
"GPU: " + $GPU >> $EXTXT
"Resolution: " + $Screens

Related

How would find the OS version of the user and state it to them with a popup window?

I need to tell the user his OS version (Windows 10 Home, Windows 7 Home, etc.). I use this code:
$WIN7H = "Microsoft Windows 7 Home"
$WIN7U = "Microsoft Windows 7 Ultimate"
$WIN7P = "Microsoft Windows 7 Professional"
$WIN7E = "Microsoft Windows 7 Enterpise"
$WIN10H = "Microsoft Windows 10 Home"
If ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) {
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("This is Windows 10 Home",0,"Windows 10",0)
}else if ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) {
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("This is Windows 7 Home",0,"Windows 7",0)
}
to find and state the user's OS version, but I get the following error in powershell:
At line:7 char:60
+ ... If ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H
...
+ ~~~
Unexpected token '-eg' in expression or statement.
At line:7 char:64
+ ... ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) {
+ ~~~~~~~
Unexpected token '$WIN10H' in expression or statement.
At line:7 char:64
+ ... ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) {
+ ~~~~~~~
Missing closing ')' after expression in 'If' statement.
At line:7 char:71
+ ... ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) {
+ ~
Unexpected token ')' in expression or statement.
At line:12 char:2
+ }else if ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $W ...
+ ~~~~
Unexpected token 'else' in expression or statement.
At line:12 char:64
+ ... if ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H)
...
+ ~~~
Unexpected token '-eg' in expression or statement.
At line:12 char:68
+ ... f ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) {
+ ~~~~~~
Unexpected token '$WIN7H' in expression or statement.
At line:12 char:68
+ ... f ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) {
+ ~~~~~~
Missing closing ')' after expression in 'if' statement.
At line:12 char:74
+ ... f ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) {
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Basically, all I need is a script to be able find the user's OS version. To be more detailed, it should be something like:
if ("The user is running windows 10") {
....something here....
}else if("He is running windows 7"){
Then show a popup that "You are running Windows 7, you need Windows 10"
(or something like that...)
}
You may be laughing at me because it is written with bad coding skills, but I am just a beginner in powershell. Sorry :)
Any help would be greatly appreciated!
Thank you!
--
MS
It should be -eq [-EQ] rather than -eg.
But if all you need is to send a popup to user if it is not Win 10 home, this is all your script needs to do:
$OS = (Get-WmiObject -class Win32_OperatingSystem).caption
if ($OS -ne "Microsoft Windows 10 Home")
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("This is $OS. You need Windows 10 Home",0,"Windows 10 Notification",0)
}

Adding 100 users in powershell script not working

I am trying to add 100 user accounts with PowerShell in my domain system in Windows. I get an error message called "You cannot call a method on a null-valued expression" when running the script for import.
Error Message:
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Downloads\user-import.ps1:37 char:9
+ $brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull*
The PowerShell Script:
Import-Module ActiveDirectory
$csvpath = $PSScriptRoot + "\produksjonsbrukereImport.csv"
if (Test-Path $csvpath) {
#$csvpath = "C:\script\produksjonsbrukereImport.csv"
$csv = Import-Csv -Delimiter "," -Path $csvpath -Encoding UTF7
$OUBasePath = ",OU=Produksjon,OU=OpMeis,DC=OpMeis,DC=local"
$logpath = "$PSScriptRoot\import-brukere-loggfil.txt"
}
foreach ($line in $csv) {
$fornavn = $line.fornavn
$etternavn = $line.etternavn
$navn = ($fornavn + " " + $etternavn)
$beskrivelse = $line.beskrivelse
$passord = $line.Passord
$avdeling = $line.avdeling
$brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring(0,3)).ToLower()
$brukernavn = $brukernavn -replace "æ", "a"
$brukernavn = $brukernavn -replace "å", "a"
$brukernavn = $brukernavn -replace "ø", "o"
$principal = $brukernavn + "#OpMeis.local"
$profPath = ($profBasePath + $brukernavn)
$profPathTrue = $profPath + ".V2"
$OU = ("OU=" + $avdeling + $OUBasePath)
}

execute ftp with white space in path

The following PowerShell script runs an ftp command but returns an error as it cannot get find the absolute source path (which includes white space and does take the complete path as "File One” and “Destination One".
Output:
/usr/bin/lftp -c \"mirror --only-missing --verbose sftp://jason#10.1.1.1:/e:/Files/File One e:/Files/Destination One/File One\"
Access failed: No such file (e:/Files/File)
The values of each variables are hardcoded and cannot be changed for a reason. In that case, what are the changes required in the $ftppath or $ftpcmd to include these white spaces in the source and dest, so it can return the ftpcmd correctly. Could someone please help?
Script:
# these values are hardcoded and cannot be modified
$root = "/e:/Files"
$source = "File One"
$dest = "e:/Files/Destination One/File One"
$user = "jason"
$server = "10.1.1.1"
# ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + $dest
$ftpcmd = '/usr/bin/lftp -c \"mirror --only-missing --verbose ' + "$ftppath"+'\"'
$ftpcmd
Invoke-Expression -command $ftpcmd
so i guess you can simply use it like thi:
# these values are hardcoded and cannot be modified
$root = "/e:/Files"
$source = "File One"
$dest = "e:/Files/Destination One/File One"
$user = "jason"
$server = "10.1.1.1"
# ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + $dest
$ftpcmd = '/usr/bin/lftp -c \"mirror --only-missing --verbose ' + "$ftppath"+'\"'
$ftpcmd
Invoke-Expression -command $ftpcmd.replace(" ", "\ ")
```shell
ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + $dest
```
replace by this,use quote wrap $dest
```shell
ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + "${dest}"
```

Issue with Powershell parsing in 4.0

I am having issues with the snippet below with Powershell 4.0 on 2012 R2:
$query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' +
$disk.DeviceID.replace('\','\\') +
'"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
On my Windows 7 laptop running Powershell 2.0 this works fine both in the Powershell cli and in a .ps1 script. In Powershell 4.0 if I run it in the cli it works fine, but if I run it out of a .ps1 script I get:
You cannot call a method on a null-valued expression.
At C:\temp\perf.ps1:94 char:1
+ $part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.rep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ( [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
After some debug I think the issue is with .replace('\\','\\\').
I have tried .replace('\\\','\\\\\\\') but I get the same error.
Full code:
$diskdrives = get-wmiobject Win32_DiskDrive | sort Index
foreach ( $disk in $diskdrives ) {
write $disk
$scsi_details = 'SCSI ' + $disk.SCSIBus + ':' +
$disk.SCSILogicalUnit + ':' +
$disk.SCSIPort + ':' +
$disk.SCSITargetID
#$part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.replace('\','\\') + '"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
$part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.replace('\\','\\\') + '"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
$partitions = #( get-wmiobject -query $part_query |
sort StartingOffset )
foreach ($partition in $partitions) {
$vol_query = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID="' +
$partition.DeviceID +
'"} WHERE AssocClass=Win32_LogicalDiskToPartition'
$volumes = #(get-wmiobject -query $vol_query)
foreach ( $volume in $volumes) {
$allDiskInfo += $($volume.name + ' ' +
$volume.FileSystem + ' ' + $disk.Index + ' '
) | Out-String -stream
} # end foreach vol
} # end foreach part
} # end foreach disk
When I declared $disks I made it [string]. What I think happened is that Powershell 2.0 coerced it back to the system object but Powershell 4.0 must have stronger typing rules and was not coercing it from what I declared it as. Once I took [string] out of the declaration, it works on 4.0.
Thanks for the responses!!

powershell error-CategoryInfo : InvalidOperation: (split:String) [], RuntimeException

$Analog_File = "c:\temp\test\mt.txt"
#(Get-Item $Analog_File).length
if((Test-Path(-not $Analog_File)) -or ((Get-Item $Analog_File).length -eq 0kb))
{
exit
}
$m = Select-String -pattern ",T" c:\temp\test\mt_1.txt
#$m.length #Get the number of rows
For ($i=0; $i -le $m.length; $i++)
{
$s1 = $m[$i] -split ":"
$s1[2]
if ([int]$s1[2] -ge 9) #le = less than, ge = greater than, eq = equal to
{
$s2 = $s1[3] -split ","
$compid = $s2[2].split('''')[0] + "." + $s2[3].split('''')[0] + "." + $s2[4].split('''')[0] + "." + $s2[5].split('''')[0]
$matches = (Select-String -pattern $compid c:\temp\test\mt_1.txt).line
#($matches.split(';')[0])
#($matches.split(';')[0]).split(',')[0]
#($matches.split(';')[0]).split(',')[1]
if ($s2[6] -eq "T")
{
#$s2[2].split('''')[1]
#"Not in Service"
#"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!0!" >> $fname
}
if ($s2[7] -eq "T")
{
#"Alarm inhibit"
#"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!1!" >> $fname
}
if ($s2[8] -eq "T")
{
#"Manual update"
#"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!2!" >> $fname
}
}
}
the error message:
You cannot call a method on a null-valued expression.
At line:17 char:31
$compid = $s2[2].split <<<< ('''')[0] + "." + $s2[3].split('''')[0] + "." + $s2[4].split('''')[0] + "." + $s2[5].split('''')[0]
CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull
what can i do??