Using $env variables remotely - issues

So I was completing a function that I want to create a xml file on the remote machines $env:tmp directory. It creates it using a scheduled task using the PSWindowsupdate module using Invoke-WUinstall.

So I create it like this:

{ $wuinstaller = New-Object -ComObject "Microsoft.Update.Installer"; $wuinstaller | export-clixml "$env:tmp\wuinstaller.xml" -Force }

When I import it:

Invoke-Command -ComputerName $computer { Import-Clixml $env:tmp\wuinstaller.xml -Force }

I get errors similar to this :

 Could not find file 'C:\Users\weiyentan\AppData\Local\Temp\wuinstaller.xml'.
    + CategoryInfo          : OpenError: (:) [Import-Clixml], FileNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportClixmlCommand
    + PSComputerName        : server01

Yet, when I do the explicitly define the directory such as

$wuinstaller = New-Object -ComObject “Microsoft.Update.Installer”; $wuinstaller | export-clixml C:\windows\Temp\wureboot.xml -Force

It works…a bit confused …any help appreciated.

EDIT:

While I was testing out my hypothesis and drafting the post I realized the problem. So I thought that I will write that down for posterity .

Basically when the invoke-wuinstall function runs, it creates a scheduled task in the destination machine. BUT it is running it in the context of the SYSTEM user account, as it is Windows Update commands that it is running.

Which means when I use Invoke-command under my context it doesn’t find it.

Hope that it is useful for someone that is having issues with wondering why things arent working properly in a similar situaton

Thanks for the follow-up!!