SOLVED

Why does Powershell say that my variables are empty or null?

%3CLINGO-SUB%20id%3D%22lingo-sub-1584357%22%20slang%3D%22de-DE%22%3EWhy%20does%20Powershell%20say%20that%20my%20variables%20are%20empty%20or%20null%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1584357%22%20slang%3D%22de-DE%22%3E%3CP%3EDear%20Techcommunity%3C%2FP%3E%3CP%3EI%20wanted%20to%20ask%20why%20my%20script%20keeps%20on%20saying%20that%20these%20variables%20are%20empty%20or%20null.%20the%20following%20variables%20are%20affected%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24pcname%20%3D%20'server01'%0A%24RDdestfile%20%3D%20%22C%3A%5CScripts%5CCert%5CCertificate_04.pfx%22%0A%24ThumbprintPath%20%3D%20'RDS%3A%5CGatewayServer%5CSSLCertificate%5CThumbprint'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BAs%20you%20can%20see%2C%20these%20variables%20clearly%20have%20something%20in%20them.%3C%2FP%3E%3CP%3EHere%20is%20where%20I%20want%20to%20use%20them%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24RemoteSession%20%3D%20New-PSSession%20-ComputerName%20%24pcname%20-Credential%20%24cred%0A%20%20%20%20%20%20%20%20Invoke-Command%20-Session%20%24RemoteSession%20-ScriptBlock%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Import-PfxCertificate%20-FilePath%20%24RDdestfile%20-CertStoreLocation%20%24CertLocation%0A%20%20%20%20%20%20%20%20%20%20%20%20%24certificate%20%3D%20Get-PfxCertificate%20-FilePath%20%24RDdestfile%0A%20%20%20%20%20%20%20%20%20%20%20%20Import-Module%20RemoteDesktopServices%0A%20%20%20%20%20%20%20%20%20%20%20%20Set-Item%20-Path%20%24ThumbprintPath%20-Value%20%24certificate.Thumbprint%20-ErrorAction%20Stop%0A%20%20%20%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20you%20can%20help%20me%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreetings%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMartin%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1584357%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1584788%22%20slang%3D%22en-US%22%3ERe%3A%20Why%20does%20Powershell%20say%20that%20my%20variables%20are%20empty%20or%20null%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1584788%22%20slang%3D%22en-US%22%3E%3CP%3EBecause%20you're%20trying%20to%20use%20them%20within%20a%20remote%20session.%20You%20either%20need%20to%20set%20them%20within%20the%20remote%20session%20or%20pass%20them%20using%20a%20script%20block%2Fthe%20%22using%22%20scope%20modifier%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_remote_variables%3Fview%3Dpowershell-7%23using-local-variables%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_remote_variables%3Fview%3Dpowershell-7%23using-local-variables%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Dear Techcommunity

I wanted to ask why my Script keeps on saying that these variables are empty or null. the following variables are affected:

 

$pcname = 'server01'
$RDdestfile = "C:\Scripts\Cert\Certificate_04.pfx"
$ThumbprintPath = 'RDS:\GatewayServer\SSLCertificate\Thumbprint'

 

 As you can see, these Variables clearly have something in them.

Here is where I want to use them:

 

$RemoteSession = New-PSSession -ComputerName $pcname -Credential $cred
        Invoke-Command -Session $RemoteSession -ScriptBlock {
            Import-PfxCertificate -FilePath $RDdestfile -CertStoreLocation $CertLocation
            $certificate = Get-PfxCertificate -FilePath $RDdestfile
            Import-Module RemoteDesktopServices
            Set-Item -Path $ThumbprintPath -Value $certificate.Thumbprint -ErrorAction Stop
        }

 

 

I hope you can help me

 

Greetings

 

Martin

1 Reply
Highlighted
Best Response confirmed by __Martin__ (Occasional Contributor)
Solution

Because you're trying to use them within a remote session. You either need to set them within the remote session or pass them using a script block/the "using" scope modifier: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_vari...