I’m executing a script remotely using a service account. I can’t run anything unless the service account is actually logged in on the server. The problem is the LOCALAPPDATA variable is blank and C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule is being executed. It’s failing on Line 95 stating -Path is NULL
If I log into the server as the service account and then execute the script remotely, it works fine. How can I get this to work using the Service Account without having to be logged in on the server? This will be a scheduled job that runs daily.
Hello, welcome to the forum. You should post what you’re trying to run that is causing this error. PowershellGet is used to install modules/scripts. I would guess you are installing or importing something and you may not need to. Also, powershellget 2.2.5 is the latest stable version, perhaps you should update that and see if that doesn’t resolve your issue.
Your title states ‘localappdata is null’. From your explanation it seems like you’re stating that localappdata envrionemtnal variable (the path) is null unless you login. I’m not able to replicate that. For example, let’s take a step back (often helpful when trying to troubleshoot) and confirm it’s truly null:
That returns my localappdata path, without being logged in via RDP or locally to $servername.
EDIT: I would say it’s possible that you have to login to create a profile potentially before that value would return properl (as in log in once to create the local profile). I was trying to find a server that I had not logged into yet in my environment with one of my admin accounts but unfortunately it looks like i’ve logged into everyone I’ve checked so far, to try and confirm that.
So as Doug mentioned, could you further clarify what exactly you’re doing and share your entire script.
I’m using Powershell to get Teams Call Usage logs. Using SQL Server, I have a job that executes as a specific Service Account. I get the error unless the job starts while I’m logged into the server as the service account.
The log would then spit out:
PS>TerminatingError(Join-Path): “Cannot bind argument to parameter ‘Path’ because it is null.”
Join-Path : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1:95 char:81
FullyQualifiedErrorId :
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand
The variable ‘$script:PSGetAppLocalPath’ cannot be retrieved because it has not been set.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1:102 char:86
One more formatting thing, even though i don’t think it’s related: you gotta be careful about those quote characters. The ones in your code block won’t be interpreted by Powershell correctly. Here’s the same code with all the incorrect quote characters replaced with correct UTF8 ones.
now for your actual problem. I only did a couple of tests by comparison where I used an account to leverage invoke-command against a couple of computers I have never used before, so I wouldn’t have a profile on them. All I did was ask it to return the $ENV:LOCALAPPDATA variable which is did correctly. Even if i run psexec interactively to launch Powershell that session running as NT AUTHORITY\SYSTEM still has a value in $ENV:LOCALAPPDATA.
My suspicion is that it’s something to do with how your script is being executed and by whom.
You said you’re running the script remotely as a service account, can you provide a little bit more detail about how you’re running it remotely?
I tried to test using Write-Output to the log to get the value of $ENV:LOCALAPPDATA at the top of my script. Here’s the output:
Transcript started, output file is \PathToMyLogFile\log-test.txt
Local App Data:
PS>TerminatingError(Join-Path): “Cannot bind argument to parameter ‘Path’ because it is null.”
Here’s the code I added to my script:
Start-Transcript -Path "\\........\log-test.txt"
Write-Output "Local App Data: "
Write-Output $ENV:LOCALAPPDATA
#Trust the repository used to prevent required user input
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
I think I need to set the environment variable because the code I’m executing is calling C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1 and the error I’m getting is because this file is referencing $env:LOCALAPPDATA in the following manner:
I just went through the code and I don’t see a reference to PSModule.psm1 so it appears this is being called automatically and I’m not sure what the purpose of it is or if there’s a way to avoid and still run my code successfully. I see it is installed on every machine that I have Powershell on, regardless if that machine has run the code that I’m trying to run here.
Searching for PSModule.psm1 in this forum, it looks like the Install-Module -Name TeamsCloudCommunicationApi -Scope CurrentUser
is calling PSModule.psm1
I was able to get the exact error message just by running the following. If I remove the Install-Module line, the error goes away, but that prevents me from installing what I need.
There’s absolutely no shame to that. We’ve ALL been there once.
You should not run code you don’t understand. You may start reading this:
… and usually you wouldn’t see any reference to the module anyway … but I’d expect there are some functions from that module in use.
Hmmm … unlikely.
What is it actually what you need? What is the general purpose of your code/script?
And just to have it at least mentioned once:
That’s a very bad idea. PowerShell is quite slow when it comes to file system operations anyway. Doing it to a network share will slow down your code even further.