Sept Win10 Update breaks PowerShell script

%3CLINGO-SUB%20id%3D%22lingo-sub-1689908%22%20slang%3D%22en-US%22%3ESept%20Win10%20Update%20breaks%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1689908%22%20slang%3D%22en-US%22%3E%3CP%3EHello%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20a%20newbie%20at%20PowerShell%20scripting.%26nbsp%3B%20I%20have%20been%20using%20the%20following%20code%20for%20the%20last%20two%20years%2C%20and%20this%20last%20week%20(September)%20it%20stopped%20working.%26nbsp%3B%20%26nbsp%3BI%20suspect%20that%20a%20windows%2010%20update%20changed%20or%20updated%20some%20aspect%20of%20powershell%2C%20and%20the%20crappy%20code%20I%20was%20using%20is%20no%20longer%20valid.%26nbsp%3B%20Can%20anyone%20point%20me%20toward%20a%20fix%2C%20proper%20syntax%2C%20or%20something%20to%20read%20that%20will%20help%20me%20clean%20this%20up%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24usrName%20%3D%20Get-WmiObject%20-Class%20Win32_Process%20-Filter%20%22Name%20%3D%20'explorer.exe'%22%20%7C%3CBR%20%2F%3EForEach-Object%20%7B%20%24_.GetOwner()%20%7C%20%25%20%7B%20%22%24(%24_.User)%22%20%7D%20%7D%20%7C%3CBR%20%2F%3ESort-Object%20-Unique%3CBR%20%2F%3Ewrite-host%20%22Logged%20in%20User%20is%3A%20%22%20-NoNewLine%3CBR%20%2F%3Ewrite-host%20(%24usrName)%3C%2FP%3E%3CP%3E%24upath%20%3D%20%22C%3A%5CUsers%22%3CBR%20%2F%3E%23%20%24usrPath%20%3D%20join-path%20-path%20%24upath%20-ChildPath%20%24usrName%3CBR%20%2F%3E%24usrPath%20%3D%20join-path%20%24upath%20%24usrName%3CBR%20%2F%3Ewrite-host%20%22Logged%20in%20User%20PathName%20is%3A%20%22%20-NoNewLine%3CBR%20%2F%3Ewrite-host%20(%24usrPath)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20errors%20when%20running%2C%20which%20only%20started%20recently%20with%20a%20windows%20update%2C%20are%20as%20follows%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPS%20C%3A%5CUsers%5Cneali%5CDesktop%26gt%3B%20C%3A%5CUsers%5Cneali%5CDesktop%5CAsset-Capture.ps1%3CBR%20%2F%3ELogged%20in%20User%20is%3A%20neali%3CBR%20%2F%3EJoin-Path%20%3A%20Cannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'System.String'%20required%20by%20parameter%20'ChildPath'.%20Specified%20method%20is%20not%20supported.%3CBR%20%2F%3EAt%20C%3A%5CUsers%5Cneali%5CDesktop%5CAsset-Capture.ps1%3A35%20char%3A29%3CBR%20%2F%3E%2B%20%24usrPath%20%3D%20join-path%20%24upath%20%24usrName%3CBR%20%2F%3E%2B%20~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidArgument%3A%20(%3A)%20%5BJoin-Path%5D%2C%20ParameterBindingException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20CannotConvertArgument%2CMicrosoft.PowerShell.Commands.JoinPathCommand%3CBR%20%2F%3E%3CBR%20%2F%3ELogged%20in%20User%20PathName%20is%3A%3CBR%20%2F%3EJoin-Path%20%3A%20Cannot%20bind%20argument%20to%20parameter%20'Path'%20because%20it%20is%20null.%3CBR%20%2F%3EAt%20C%3A%5CUsers%5Cneali%5CDesktop%5CAsset-Capture.ps1%3A39%20char%3A31%3CBR%20%2F%3E%2B%20%24ScratchDir%20%3D%20join-path%20-path%20%24usrPath%20-ChildPath%20%22downloads%5CCopyTemp%20...%3CBR%20%2F%3E%2B%20~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidData%3A%20(%3A)%20%5BJoin-Path%5D%2C%20ParameterBindingValidationException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ParameterArgumentValidationErrorNullNotAllowed%2CMicrosoft.PowerShell.Commands.JoinPathCommand%3CBR%20%2F%3E%3CBR%20%2F%3ETest-Path%20%3A%20Cannot%20bind%20argument%20to%20parameter%20'Path'%20because%20it%20is%20null.%3CBR%20%2F%3EAt%20C%3A%5CUsers%5Cneali%5CDesktop%5CAsset-Capture.ps1%3A40%20char%3A22%3CBR%20%2F%3E%2B%20if(!(Test-Path%20-Path%20%24ScratchDir%20))%7B%3CBR%20%2F%3E%2B%20~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidData%3A%20(%3A)%20%5BTest-Path%5D%2C%20ParameterBindingValidationException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ParameterArgumentValidationErrorNullNotAllowed%2CMicrosoft.PowerShell.Commands.TestPathCommand%3CBR%20%2F%3E%3CBR%20%2F%3EJoin-Path%20%3A%20Cannot%20bind%20argument%20to%20parameter%20'Path'%20because%20it%20is%20null.%3CBR%20%2F%3EAt%20C%3A%5CUsers%5Cneali%5CDesktop%5CAsset-Capture.ps1%3A47%20char%3A30%3CBR%20%2F%3E%2B%20%24AssetPath%20%3D%20join-path%20-path%20%24usrPath%20-ChildPath%20%24AssetPartial%3CBR%20%2F%3E%2B%20~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidData%3A%20(%3A)%20%5BJoin-Path%5D%2C%20ParameterBindingValidationException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ParameterArgumentValidationErrorNullNotAllowed%2CMicrosoft.PowerShell.Commands.JoinPathCommand%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%20for%20any%20guidance%2C%20mentoring%2C%20corrections%2C%20or%20hints.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENeal%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1689908%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1690488%22%20slang%3D%22en-US%22%3ERe%3A%20Sept%20Win10%20Update%20breaks%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1690488%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F800837%22%20target%3D%22_blank%22%3E%40nealix%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20I%20can%20see%20from%20the%20error%3C%2FP%3E%3CP%3E%3CSTRONG%3ECannot%20convert%20'System.Object%5B%5D'%20to%20the%20type%20'System.String'%20required%20by%20parameter%20'ChildPath'.%20Specified%20method%20is%20not%20supported%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20it%20seems%20that%20the%20value%20passed%20to%20%24usrName%20is%20not%20a%20string%2C%20it%20seems%20that%20you%20are%20fetching%20this%20value%20from%20another%20source%20and%20powershell%20is%20failing%20in%20dealing%20with%20it.%3C%2FP%3E%3CP%3E1-%20Can%20you%20try%20type%20the%20path%20manually%20and%20check%20if%20it%20will%20work%3C%2FP%3E%3CP%3E2-%20what%20is%20the%20output%20for%20%3CSTRONG%3E%24usrName.gettype()%20%3C%2FSTRONG%3Eand%20%3CSTRONG%3E%24upath.gettype()%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted

@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()

Highlighted

@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

 

Highlighted

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

Highlighted

@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)