Unwanted newline when accessing win32_LogicalDisk

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Unwanted newline when accessing win32_LogicalDisk

Post by owinsloe »

Afternoon,
I'm running current PSS and on a Windows 10 computer.

I am experiencing an unusual issue when gathering output from the win32_logicaldisk class.

When I run the code through PSS the output appears as I would expect. When I compile the code and run the output the return value on the object's "free' property causes a newline to display. See below for code and examples (problem is "$FreeSpace = $obj.free").

I have tried various things to eliminate the newline trap\catch, trimming the value etc but can not suppress it?

Any suggestions?

Thanks



Code
$script:drive = Get-Wmiobject -class Win32_LogicalDisk | Where-Object { $_.drivetype -eq 3 } | ForEach-Object { get-psdrive $_.deviceid[0] }
$script:drive | foreach-object {
$Obj = $_
Write-Host here-1
$MyDrive = $obj.name
Write-Host here-2
$FreeSpace = $obj.free
Write-Host here-3
$UsedSpace = $obj.used
Write-Host here-4
[int64]$Totalfree = $FreeSpace /1GB
Write-Host here-5
[int64]$Totalused = $UsedSpace /1GB
}

"Drive=$MyDrive, freespace=${Totalfree}GB, usedspace=${Totalused}GB"


PSS Consol
>> Platform: V5 64Bit (STA)
here-1
here-2
here-3
here-4
here-5
Drive=C, freespace=158GB, usedspace=80GB

*** PowerShell Script finished. ***
>> Execution time: < 1 second
>> Script Ended



Run as an EXE on CMD consol
H:\>c:\cardinal\test.exe
here-1
here-2

here-3
here-4
here-5
Drive=C, freespace=158GB, usedspace=80GB
cody m

Re: Unwanted newline when accessing win32_LogicalDisk

Post by cody m »

So I have a temporary fix until I can do some more research into what is actually causing this to happen. Here is what i did to the code.

Code: Select all

$script:drive = Get-Wmiobject -class Win32_LogicalDisk | Where-Object { $_.drivetype -eq 3 } | ForEach-Object { get-psdrive $_.deviceid[0] }
$script:drive | foreach-object {
	Write-Host here-1
	$MyDrive = $_.name
	Write-Host here-2 -NoNewLine
	$FreeSpace = $_.free
	Write-Host here-3
	$UsedSpace = $_.used
	Write-Host here-4
	[int64]$Totalfree = $FreeSpace /1GB
	Write-Host here-5
	[int64]$Totalused = $UsedSpace /1GB
}
"Drive=$MyDrive, freespace=${Totalfree}GB, usedspace=${Totalused}GB"
I just added the NoNewLine parameter to the second Write-Host. Also I got rid of the variable $Obj since it isn't necessary to re-assign the value of $_ to an intermediate variable. I will update you later when I figure out what is causing this.
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: Unwanted newline when accessing win32_LogicalDisk

Post by owinsloe »

Hey thanks for that. Yes I was using $_ but added the $Obj as I thought that maybe the $_.free was being interpreted as some internal reserved variable. All good, will give that a whirl.
Cheers
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Unwanted newline when accessing win32_LogicalDisk

Post by mxtrinidad »

I did get the same results you describe but the code needed some improvements.

The for-each could be change to use the select-object, which is the one (and must use) effective way to got thru the psobject. Then, the output formatting can vary (look at .NET Documentation on String formatting: https://docs.microsoft.com/en-us/dotnet ... at-strings )

Here's an update variation of your sample code:

$script:drive = Get-Wmiobject -class Win32_LogicalDisk | Where-Object { $_.drivetype -eq 3 } `
| ForEach-Object { get-psdrive $_.deviceid[0] }

$script:drive | Select-Object $_ | Format-List

$script:drive | Select-Object @{ label = "Drive"; Expression = { $_.name } }, `
@{ label = "freespace_GB"; Expression = { ($_.free/1GB).ToString("000") } }, `
@{ label = "usedspace_GB"; Expression = { ($_.used/1GB).ToString("000") } } `
| Format-Table -AutoSize;

# results: (executed exe)
PS C:\temp> .\TestWMIDrive2.exe

Name : C
Description : Windows
Provider : Microsoft.PowerShell.Core\FileSystem
Root : C:\
CurrentLocation : temp

Name : D
Description : TOSHIBA EXT
Provider : Microsoft.PowerShell.Core\FileSystem
Root : D:\
CurrentLocation :

Drive freespace_GB usedspace_GB
----- ------------ ------------
C 722 230
D 820 1043


PS C:\temp>

Now, If you still wondering about the Write-Host issue... it was a matter of polishing the code.

$script:drive | foreach-object {
Write-Host here-1
$_.name
Write-Host here-2
$_.free.ToString()
Write-Host here-3
$_.used.ToString()
Write-Host here-4
[int64]$Totalfree = $FreeSpace /1GB
$Totalfree.ToString()
Write-Host here-5
[int64]$Totalused = $UsedSpace /1GB
$Totalused.ToString()
}

Hope this helps,
:)
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: Unwanted newline when accessing win32_LogicalDisk

Post by owinsloe »

Hmmm, (thanks BTW for looking at this)....I tried using a variation of your example and something still not quite right. I want to store just the drive letter is a simple has for reuse later in my script thus the &script:drive.

It now looks like gwmi is causing a newline, possibly because I'm not using the correct select query. So my code and results are as follows;

$script:drive=@{ }
$win32Drive = Get-Wmiobject -class Win32_LogicalDisk | Where-Object { $_.drivetype -eq 3 } `
| ForEach-Object { get-psdrive $_.deviceid[0] }
Write-Host here-1
$win32Drive | Select-Object @{ label = "Drive"; Expression = { $_.name } }, `
@{ label = "freespace"; Expression = { ($_.free/1GB).ToString("") -as [int]} }, `
@{ label = "usedspace"; Expression = { ($_.used/1GB).ToString("") -as [int]} } | ForEach-Object {
$hdd = $_.Drive
$script:drive.$hdd = $true
$LogText = 'Drive=' + $_.Drive + ', freespace=' + $_.freespace + 'GB, usedspace=' + $_.usedspace + 'GB'
Write-Host here-2
$LogText
Write-Host here-3
}




Results
H:\>c:\cardinal\test.exe
here-1

here-2
Drive=C, freespace=157GB, usedspace=81GB
here-3
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Unwanted newline when accessing win32_LogicalDisk

Post by Alexander Riedel »

I was able to replicate your initial results. I am tempted to say that there is something in the host for the command line package that doesn't remove a newline when it should.
Building it Windows Application host does not show the problem. So I will investigate this and see what I can find out.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Unwanted newline when accessing win32_LogicalDisk

Post by Alexander Riedel »

The errant newline comes from a progress message that only contains a space.
Some module reports that it finished loading with no status text.
Preface your script with
$progresspreference='SilentlyContinue'
and it will go away.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: Unwanted newline when accessing win32_LogicalDisk

Post by owinsloe »

Thanks Alexander.

I captured the bytes back and compared EXE v ps1 runs and saw the same thing but could not trim or -replace the results.

Became very frustrating. Have tried your suggestion and that did resolve the issue.

Thanks again.
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: Unwanted newline when accessing win32_LogicalDisk

Post by owinsloe »

Brilliant...as a side effect of setting '$progresspreference = 'SilentlyContinue'' it also got rid of the annoying result display from Invoke-WebRequest that I tried heaps of things to avoid but couldn't. so thanks for that!!

# Test internet connectivity
$Results = @()
$URI = "www.whatismyip.com"
Log "Perform Internet connection tests to domain = $URI"

foreach ($uriToCheck in @("http://$URI", "https://$URI"))
{
try
{
$request = Invoke-WebRequest -URI $uriToCheck -usebasicparsing -timeoutsec 5 | out-null
$StatusCode = $request.StatusCode
$Results += "Connection:$uriToCheck StatusCode:$StatusCode"
Log " Connection:$uriToCheck StatusCode:$StatusCode"
}
catch
{
$Results += "Connection:$uriToCheck StatusCode:Failed to connect"
Log " Connection:$uriToCheck StatusCode:Failed to connect"
}
}
This topic is 6 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.