Using Remote $env user variable with invoke-command not working

%3CLINGO-SUB%20id%3D%22lingo-sub-1475280%22%20slang%3D%22en-US%22%3EUsing%20Remote%20%24env%20user%20variable%20with%20invoke-command%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1475280%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20short%2C%20the%20script%20below%20is%20taking%20the%20%24env%3AAPPDATA%20info%20from%20my%20local%20computer%20instead%20of%20the%20%24env%3AAPPDATA%20info%20for%20the%20user%20currently%20logged%20on%20at%20remote%20computer%2C%20specified%20in%20the%20%24computer%20variable.%20I%20have%20tried%20with%20no%20luck%20to%20search%20this%20in%20Google%20so%20I%20thought%20I%20would%20post%20here%20to%20see%20if%20anyone%20can%20help.%20Driving%20me%20crazy.%20Let%20me%20know%20if%20anything%20doesn't%20make%20sense.%3C%2FP%3E%3CP%3EThanks%20in%20advance!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24computer%20%3D%20read-host%20'Enter%20Computer%20Name'%0Ainvoke-command%20-computer%20%24computer%20-scriptblock%20%7BRemove-Item%20-recurse%20%22%24env%3AAPPDATA%5CMicrosoft%5CTeams%5Cblob_storage%5C*%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1475280%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-1479750%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20Remote%20%24env%20user%20variable%20with%20invoke-command%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1479750%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531473%22%20target%3D%22_blank%22%3E%40charlie4872%3C%2FA%3E%26nbsp%3B%20Hello%20Charlie.%20You%20can%20try%20to%20get%20the%20variable%20from%20the%20remote%20machine%20with%20this.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24computer%20%3D%20read-host%20'Enter%20Computer%20Name'%0Ainvoke-command%20-computer%20%24computer%20-scriptblock%20%7BRemove-Item%20-recurse%20%22%24(%5BEnvironment%5D%3A%3AGetEnvironmentVariable(%E2%80%9CAPPDATA%E2%80%9D))%5CMicrosoft%5CTeams%5Cblob_storage%5C*%22%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%20Moreno%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1493704%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20Remote%20%24env%20user%20variable%20with%20invoke-command%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1493704%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531473%22%20target%3D%22_blank%22%3E%40charlie4872%3C%2FA%3E%3C%2FP%3E%3CP%3EThe%20variable%20APPDATA%20is%20a%20user%20enviroment%20variable%20and%20is%20present%20only%20in%20the%20user%20session...%3C%2FP%3E%3CP%3EAPPDATA%20is%20builded%20runtime%2C%20in%20the%20user%20session!%3C%2FP%3E%3CP%3ESo%20if%20you%20invoke%20locally%20(or%20remotely)%20your%20code%20or%20the%20following%20line%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EGet-CimInstance%20-ClassName%20win32_environment%20%7CWhere%7B%24_.name%20-match%20%22appdata%22%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eyou%20get%20nothing%2C%20and%20is%20the%20correct%20final%20result%3C%2FP%3E%3CP%3EThe%20%22no%20default%22%20user%20variables%20are%20present%20in%20the%20registry%20in%20Computer%5CHKEY_CURRENT_USER%5CEnvironment%26nbsp%3B%20(appdata%20is%20not%20present)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22varia.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F201669iC1C85FD55221EB67%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22varia.png%22%20alt%3D%22varia.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAPPDATA%20is%20builded%20using%20%25USERPROFILE%25%20(session%20variable)%20and%20the%20string%20%5CAppData%5CRoaming%3C%2FP%3E%3CP%3ESo%20you%20are%20approching%20the%20problem%20in%20the%20wrong%20way%2C%20that%20is%20the%20reason%20you%20getting%20crazy...%3C%2FP%3E%3CP%3EHow%20to%20resolve%20the%20problem%3F%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet%20the%20remote%20users%20profile%3C%2FSTRONG%3E%20(or%20a%20single%20remote%20user)%20%3CSTRONG%3Ebuild%20the%20appdata%3C%2FSTRONG%3E%20and%20remove%20the%20teams%20blob%20storage%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23returning%20%22remote%22%20builded%20appdata%20variables%0A(Get-CimInstance%20-ClassName%20win32_userprofile%20-ComputerName%20%24computer%20%7Cselect%20localpath)%7C%20%25%7B%20%22%7B0%7D%5Cappdata%5Croaming%22%20-f%20%24_.localpath%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBye%20Gas%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1493707%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20Remote%20%24env%20user%20variable%20with%20invoke-command%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1493707%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20a%20wrong%20concept%20and%20a%20wrong%20code%20as%20I%20explained%26nbsp%3B%20to%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531473%22%20target%3D%22_blank%22%3E%40charlie4872%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBye%20gas%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

In short, the script below is taking the $env:APPDATA info from my local computer instead of the $env:APPDATA info for the user currently logged on at remote computer, specified in the $computer variable. I have tried with no luck to search this in Google so I thought I would post here to see if anyone can help. Driving me crazy. Let me know if anything doesn't make sense.

Thanks in advance!

 

 

$computer = read-host 'Enter Computer Name'
invoke-command -computer $computer -scriptblock {Remove-Item -recurse "$env:APPDATA\Microsoft\Teams\blob_storage\*}

 

 

 

3 Replies

@charlie4872  Hello Charlie. You can try to get the variable from the remote machine with this. 

$computer = read-host 'Enter Computer Name'
invoke-command -computer $computer -scriptblock {Remove-Item -recurse "$([Environment]::GetEnvironmentVariable(“APPDATA”))\Microsoft\Teams\blob_storage\*"}

 

Regards

Erick Moreno 

@charlie4872

The variable APPDATA is a user enviroment variable and is present only in the user session...

APPDATA is builded runtime, in the user session!

So if you invoke locally (or remotely) your code or the following line

 

Get-CimInstance -ClassName win32_environment |Where{$_.name -match "appdata"}

 

you get nothing, and is the correct final result

The "no default" user variables are present in the registry in Computer\HKEY_CURRENT_USER\Environment  (appdata is not present)

 

varia.png

 

APPDATA is builded using %USERPROFILE% (session variable) and the string \AppData\Roaming

So you are approching the problem in the wrong way, that is the reason you getting crazy...

How to resolve the problem?

Get the remote users profile (or a single remote user) build the appdata and remove the teams blob storage

 

#returning "remote" builded appdata variables
(Get-CimInstance -ClassName win32_userprofile -ComputerName $computer |select localpath)| %{ "{0}\appdata\roaming" -f $_.localpath}

 

 

Bye Gas

 

@Erick A. Moreno R. 

Is a wrong concept and a wrong code as I explained  to @charlie4872 

Bye gas