Zimi
Zimi

Reputation: 1

Can PowerShell tell the difference between on how its executed? (Scheduled Tasks -vs- Console)

Pertaining to PowerShell profiles, and storing command history to a file:

In my profile.ps1 I have:

if ($Host.Name -eq 'ConsoleHost')
{
    Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistoryFilePath } | Out-Null
}

to save my commands to a file.

However, any scheduled task I have that calls PowerShell, it stores the executed command in that history file. I am hoping to change the if statement to something smarter.

Upvotes: 0

Views: 63

Answers (2)

ThinkTankTed
ThinkTankTed

Reputation: 11

The setting is restricted to your profile, correct? So use a different account to run powershell scheduled tasks, the default is to use the system account, if you didn't change the user to your account for any tasks you should be good to go without the If statement.

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174555

Best bet is probably to check whether the current host implements the IHostSupportsInteractiveSession interface:

if($host -is [System.Management.Automation.Host.IHostSupportsInteractiveSession])
{
    Register-EngineEvent ...
}

Upvotes: 0

Related Questions