Sept Win10 Update breaks PowerShell script

Copper Contributor

Hello:

 

I am a newbie at PowerShell scripting.  I have been using the following code for the last two years, and this last week (September) it stopped working.   I suspect that a windows 10 update changed or updated some aspect of powershell, and the crappy code I was using is no longer valid.  Can anyone point me toward a fix, proper syntax, or something to read that will help me clean this up?

 

$usrName = Get-WmiObject -Class Win32_Process -Filter "Name = 'explorer.exe'" |
ForEach-Object { $_.GetOwner() | % { "$($_.User)" } } |
Sort-Object -Unique
write-host "Logged in User is: " -NoNewLine
write-host ($usrName)

$upath = "C:\Users"
# $usrPath = join-path -path $upath -ChildPath $usrName
$usrPath = join-path $upath $usrName
write-host "Logged in User PathName is: " -NoNewLine
write-host ($usrPath)

 

The errors when running, which only started recently with a windows update, are as follows;

 

PS C:\Users\neali\Desktop> C:\Users\neali\Desktop\Asset-Capture.ps1
Logged in User is: neali
Join-Path : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ChildPath'. Specified method is not supported.
At C:\Users\neali\Desktop\Asset-Capture.ps1:35 char:29
+ $usrPath = join-path $upath $usrName
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Join-Path], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.JoinPathCommand

Logged in User PathName is:
Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\neali\Desktop\Asset-Capture.ps1:39 char:31
+ $ScratchDir = join-path -path $usrPath -ChildPath "downloads\CopyTemp ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\neali\Desktop\Asset-Capture.ps1:40 char:22
+ if(!(Test-Path -Path $ScratchDir )){
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\neali\Desktop\Asset-Capture.ps1:47 char:30
+ $AssetPath = join-path -path $usrPath -ChildPath $AssetPartial
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

 

Thanks in advance for any guidance, mentoring, corrections, or hints.

 

Neal

 

4 Replies

@nealix 

As I can see from the error

Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ChildPath'. Specified method is not supported

The it seems that the value passed to $usrName is not a string, it seems that you are fetching this value from another source and powershell is failing in dealing with it.

1- Can you try type the path manually and check if it will work

2- what is the output for $usrName.gettype() and $upath.gettype()

@farismalaeb 

 

OK, I tried your suggested steps.

For the Username, $usrName.gettype() was a system object, not a string.

So I added a pipe to | Out-String, and it does convert $usrName to a string, but
prepends a newline.

 

So, for example, if $upath = "C:\Users",  and $usrName is Bill, and then I try to do a 

  $usrPath = join-path -path $upath -ChildPath $usrName

instead of the expected C:\Users\Bill, I instead get 

C:\Users

Bill

 

Do you know how I get rid of that extra newline?   The latest version of PowerShell will
not accept -NoNewLine after | Out-String

 

Let me demonstrate the problem another way.   Take this snippet of code;

#-----------

$usrName = Get-WmiObject -Class Win32_Process -Filter "Name = 'explorer.exe'" |
ForEach-Object { $_.GetOwner() | % { "$($_.User)" } } |
Sort-Object -Unique | Out-String
write-host "Logged in User is: " -NoNewLine
write-host ($usrName)

#----------

 

and run it in PowerShell.   The printed username should be right after the  

write-host "Logged in User is: " -NoNewLine

but it is not.   Instead, it always prints the username on the next line 

AFTER write-host "Logged in User is: " -NoNewLine

 

Any ideas on what changed in the last few weeks of win updates?

 

Neal

 

To share and try to help others that find this thread:

 

No one offered any real solution.  So in the end, I stopped trying to use:

 

  $usrName = Get-WmiObject -Class Win32_Process -Filter "Name = 'explorer.exe'" |
  ForEach-Object { $_.GetOwner() | % { "$($_.User)" } } |
  Sort-Object -Unique | Out-String

 

to get the logged in username, and instead switched to the following code, which works fine,
provided the output as a string, and did not have the extra newline like the Out-String command:

 

# Get current user
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

# Set the variable to the first string before the "\" character
$CurrentDomainName = $CurrentUser.split("\")[0]
#  Set the variable to the second string after the "\" character
$CurrentUserName = $CurrentUser.split("\")[1]
write-host "Logged in User is: " -NoNewLine
write-host ($CurrentUserName)

$upath = "C:\Users"
$usrPath = join-path -path $upath -ChildPath $CurrentUserName

write-host "Logged in User PathName is: " -NoNewLine
write-host ($usrPath)

 

The above works just fine.

 

Neal

@nealix 

if the main requirement is to get the username, why dont you try 

$env:USERNAME

it's an environment variable which is applicable everywhere

$usrName = $env:USERNAME
write-host "Logged in User is: " -NoNewLine
write-host ($usrName)