New
#21
Well, I have REALLY had some fun [ and learnt a lot ] manipulating data and output.
This thread started out as a general PowerShell to CMD Prompt or Batch Script conversion question.
Then I thought about it, and I decided to turn this into a project for myself where I could gain knowledge and experience for future use while also creating something informative and useful, and as it turned out, it involved some quite complex calculations and data manipulations.
I might actually start a new thread and post it for general use.
Here are some of the things that I performed.
- Converted flat numerical data output [ Bytes ] into Megabytes [ Calculation ] with a comma for the thousands and set to no decimal places, with the text MB after the formatted value..
- Converted flat numerical data output [ Bytes ] into Gigabytes [ Calculation ] with a decimal point and set to two decimal places, with the text GB after the formatted value.
- This will make you laugh. One output showed 'Normal boot', and my OCD kicked in, so I did no more than change it to 'Normal Boot' using the replacement method if it was found.
- Specified specific text output dependant on the results returned.
- I have separated values and text output.
- I have separated values and text output, and then injected further values or text.
- I have extracted values and text from a string output rather than having the full output of the values or text.
- I have used a variety of commands such as
findstr
,Replace
,-Like
,-ExpandProperty
,[Math]::Round
,.ToString
,Where
,-eq 'True'
, to name a few.- I have used two variables in a single command to produce the required output.
There were MANY different types of Commands, and they all worked, but when I looked at them, I noticed that there were small differences in the way that I had created [ Built ] them, therefore making it harder to to read because of the lack of continuity.
Anyway, I spent some time looking at them, and then I spent some more time experimenting with them, and I came to a very simple working solution, obviously all part of the learning process. I figured out that when converting PS to CMD Prompt or Batch Script, that you ONLY need to use speech marks [ Quotes ]"
at the very beginning and the very end of the command, and use an apostrophe'
inside for text, labels, or mathematical operators etc. Plus, I noticed that this appeared to allow the code to run quicker.
Anyway, before I write a novel, I will leave it there, and I hope that this will help anyone that come across, or is following this thread.
A special thanks goes to @das10 for his time, expertise, and great input, it is appreciated.
Last edited by Paul Black; 22 Feb 2022 at 07:23.
Hello @das10,
Thank you for the update.
When I was streamlining the Commands, I did actually do away with theSelect
portion in the Commands with a single selection where possible, replacing it with()
and the.
as shown in your code above. That among other changes makes it so much more readable.
Thanks again.
Just when I thought that I had pretty much everything covered, I came across one I just canNOT
seem to get right.
I have this which works . . .
Code:PowerShell "[System.Environment]::OSVersion | Format-Table -Autosize @{L='Platform';E={($_.Platform)};A='Left'}, ServicePack, Version, VersionString | Format-Table -Autosize | Out-String -Width 1000" Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT 10.0.18363.0 Microsoft Windows NT 10.0.18363.0
Now I want to addNot Available
to theServicePack
like this . . .
Code:Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT Not Available 10.0.18363.0 Microsoft Windows NT 10.0.18363.0
This is what I have tried in PS ISE [ I can convert it after it works ]. I have just used theServicePack
Parameter to get it to work, I can then add it to the original above . . .
Code:[System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {($_ServicePack)} [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {($_ServicePack -eq $_ServicePack)} [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {Write-Host ($_ServicePack)} [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq '' ) {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack} [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq $True) {$_ServicePack -eq 'Not Available'} else {$_ServicePack} [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available' | select $_ServicePack -ExpandProperty $_ServicePack} else {$_ServicePack -eq $_ServicePack} [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack}; select $_ServicePack -ExpandProperty $_ServicePack [System.Environment]::OSVersion | Format-Table -Autosize; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack}; select $_ServicePack -Expand $_ServicePack
Code:[System.Environment]::OSVersion; if ($_ServicePack -eq ' ') {$_ServicePack -eq 'Not Available'} else {$_ServicePack -eq $_ServicePack} Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT 10.0.18363.0 Microsoft Windows NT 10.0.18363.0 True
I tried to find out what the cell actually contained using the following in PS ISE . . .
Code:$MyString = '[System.Environment]::OSVersion.ServicePack' if($MyString) { Write-Host "The String is NOT EMPTY" } else { Write-Host "The String is EMPTY or NULL" } $MyString = '[System.Environment]::OSVersion.ServicePack' $a = "[System.Environment]::OSVersion.ServicePack" if ($a) { Write-Output "Variable contains Value" } else { Write-Output "Variable is Empty" } $MyString = '[System.Environment]::OSVersion.ServicePack' if([String]::IsNullOrWhiteSpace($MyString)) { Write-Host "The String is NULL or EMPTY or it has WHITESPACE" } else { Write-Host "The String is NOT EMPTY" }
It reports that theString
isNOT
Empty
, so what is it?
Code:$a = '[System.Environment]::OSVersion.ServicePack' if ($a) { Write-Output "Variable contains Value" } else { Write-Output "Variable is Empty" } Variable contains Value
Thanks.
Last edited by Paul Black; 25 Feb 2022 at 09:49.
I am afraid, I have no idea. Even running various get information commands like shown below, I can't see amy mention of just the "ServicePack" information like it shows with your command.
eg:
Code:Get-ComputerInfo -Property * or Get-CimInstance -Class CIM_OperatingSystem -ErrorAction Stop | Select *
Thanks @das10,
I couldn't find anything else that showed it either. To be honest, I was just interested for the future, in case I ever came across a similar scenario.
I amNOT
actually going to include it in the list of Commands anyway, it was just the fact that it was there and I thought I would give it a go.
Anyone that knows me, knows that I amNOT
someone who will be beaten.
Therefore, I decided to have ANOTHER go and see if I could crack it, andBINGO
.
For anyone that is interested, here it is . . .
Equal to ' '
Code:PowerShell "[System.Environment]::OSVersion | Format-Table -Autosize @{L='ServicePack';E={;if($_.ServicePack -eq '') {'Not Available'} else {'xxx'}}}" ServicePack ----------- Not Available
NOT Equal to ' '
Code:PowerShell "[System.Environment]::OSVersion | Format-Table -Autosize @{L='ServicePack';E={;if($_.ServicePack -ne '') {'Not Available'} else {'xxx'}}}" ServicePack ----------- xxx
I usedxxx
in theelse
statement as a way to show that it worked as there isNO
actual value available. You would obviously replace thexxx
with$_.ServicePack
.
Paul, on a Windows XP VBox, this is what I got:
Also, using P_Ise in the XP VBox:Code:C:\>%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe "[System.Environment]::OSVersion Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT Service Pack 3 5.1.2600.196608 Microsoft Window... C:\>%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe "[System.Environment]::OSVersion | Format-Table -Autosize @{L='ServicePack';E={;if($_.ServicePack -ne '') {'Not Available'} else {'$_.ServicePack'}}}" ServicePack ----------- Not Available
Code:Write-Host `n--------------------------- ENV Platform--------------------------- [environment]::OSVersion.Platform Write-Host `n--------------------------- ENV ServicePack------------------------ [environment]::OSVersion.ServicePack Write-Host `n--------------------------- ENV Version---------------------------- [environment]::OSVersion.Version Write-Host `n--------------------------- ENV VersionString---------------------- [environment]::OSVersion.VersionString --------------------------- ENV Platform--------------------------- Win32NT --------------------------- ENV ServicePack------------------------ Service Pack 3 --------------------------- ENV Version---------------------------- Major Minor Build Revision ----- ----- ----- -------- 5 1 2600 196608 --------------------------- ENV VersionString---------------------- Microsoft Windows NT 5.1.2600 Service Pack 3
======================================================================
Also using P_Ise on XP and Win11:
Code:XP VBox: $MyString = [System.Environment]::OSVersion.ServicePack if($MyString -eq "") { Write-Host "The String is EMPTY" } else { Write-Host $MyString } = Service Pack 3 _______________________________________________________ and on Win11: $MyString = [System.Environment]::OSVersion.ServicePack if($MyString -eq "") { Write-Host "The String is EMPTY" } else { Write-Host $MyString } = The String is EMPTY ________________________________________________________
That's good then, thanks.
I have an interesting one that I canNOT
seem to figure out.
This works in PS ISE . . .
Code:Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}} Name : Wi-Fi Interface Description : Qualcomm Atheros AR9285 Wireless Network Adapter Connector Present : True
This works in CMD Prompt . . .
Code:PowerShell "Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}" Name : Wi-Fi Interface Description : Qualcomm Atheros AR9285 Wireless Network Adapter Connector Present : True
I just canNOT
get it to work in a Script . . .
Code:PowerShell "Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}" '"Get-NetAdapter | Where-Object {$_.Name -eq 'Wi-Fi'} | Format-List @{L=' Name';E={$_.Name}}, @{L=' Interface Description';E={$_.InterfaceDescription}}, @{L=' Connector Present';E={$_.ConnectorPresent}}"' is not recognized as an internal or external command, operable program or batch file.
Am I having aSenior
moment?