Порожден неинтерактивный процесс power shell

You can start PowerShell 5 (powershell.exe) and PowerShell 7 (pwsh.exe) and specify parameters. This blog post will show you the parameters I use most for version 5 or 7.

Порожден неинтерактивный процесс power shell

These parameters work in PowerShell 5 and 7

Command

C:\Users\HarmVeenstra>pwsh.exe -Command "& {Get-ChildItem c:\scripts}" Directory: C:\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5-11-2023 16:26 Sync
-a--- 1-2-2024 20:02 1542 365healthstatus.ps1
-a--- 27-6-2022 12:37 100 accounts.csv
-a--- 8-12-2023 13:44 194 Additional_Requirement.ps1
-a--- 12-10-2022 22:43 109 hyperv.cmd
-a--- 9-10-2022 19:23 882 log.txt
-a--- 27-6-2022 12:39 329 Manager.ps1
-a--- 14-7-2023 11:18 259 screensaver.ps1
-a--- 21-10-2022 13:51 1091 Set-CorrectHyperVExternalSwitchAdapter.ps1
-a--- 1-10-2023 16:39 1929 test.ps1

ExecutionPolicy

This is used the most, I guess. 🙂 With the -ExecutionPolicy, you can specify whether the prompt or script will start in AllSigned, Bypass, RemoteSigned, Restricted, Undefined, or Unrestricted mode. I usually use the Bypass option. For example:

C:\Users\HarmVeenstra>pwsh.exe -ExecutionPolicy Bypass -noprofile
PowerShell 7.4.1
PS C:\Users\HarmVeenstra>

File

The -File parameter specifies the file to execute with either powershell.exe or pwsh.exe. With pwsh.exe, it doesn’t matter where you put the parameter. With powershell.exe, the -File parameter must be specified as the last parameter because everything behind it will be interpreted as part of the script path.

The parameter is also the default parameter if no other parameters are specified. (You don’t have to specify it in that case)

C:\Users\HarmVeenstra>powershell.exe -File c:\scripts\check_ad.ps1

NonInteractive

The -NonInteractive parameter starts a PowerShell session that will terminate when encountering a prompt instead of being stuck and running forever. You want to use this in Scheduled Tasks or CI/CD pipelines. For example:

C:\Users\HarmVeenstra>pwsh.exe -NonInteractive
PowerShell 7.4.1
PS C:\Users\HarmVeenstra> Read-Host
Read-Host: PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.

Noprofile

The -NoProfile parameter skips loading your PowerShell profile. This can be useful if you have a lot of settings in there or load modules that are not needed for the script or command that you want to start using powershell.exe or pwsh.exe. Example:

C:\Users\HarmVeenstra>pwsh.exe -NoProfile
PowerShell 7.4.1
PS C:\Users\HarmVeenstra>

WindowStyle

C:\Users\HarmVeenstra>pwsh.exe -File c:\scripts\update_network_settings.ps1 -WindowStyle Hidden

Version specific parameters

PowerShell 5

PSConsoleFile

The -PSConsoleFile parameter can be used to specify a .psc1 file. This file contains all the PSSnapins that should be loaded when starting a new PowerShell prompt. First, start a PowerShell prompt, load all the PSSnapins you want, and then use Export-Console -Path C:\Scripts\Console.psc1 to save those to a file. The file contains something like this:

<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0"> <PSVersion>5.1.26058.1000</PSVersion> <PSSnapIns> <PSSnapIn Name="Microsoft.BDD.PSSnapIn" /> </PSSnapIns>
</PSConsoleFile>

Then you can start a new session and specify that .psc1 file:

PS C:\Users\HarmVeenstra> powershell.exe -PSConsoleFile C:\scripts\Console.psc1
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
Loading personal and system profiles took 7217ms.
PS C:\Users\HarmVeenstra> Get-PSSnapin
Name : Microsoft.PowerShell.Diagnostics
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains Windows Eventing and Performance Counter cmdlets.
Name : Microsoft.PowerShell.Host
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-Transcript and Stop-Transcript) that are provided for use with the Windows PowerShell console host.
Name : Microsoft.PowerShell.Core
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.
Name : Microsoft.PowerShell.Utility
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains utility cmdlets that are used to view and organize data in different ways.
Name : Microsoft.PowerShell.Management
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell Snap-In contains management cmdlets that are used to manage Windows components.
Name : Microsoft.PowerShell.Security
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell Snap-In contains cmdlets to manage Windows PowerShell security.
Name : Microsoft.WSMan.Management
PSVersion : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains cmdlets (such as Get-WSManInstance and Set-WSManInstance) that are used by the Windows PowerShell host to manage WSMan operations.
Name : Microsoft.BDD.PSSnapIn
PSVersion : 1.0
Description : This Microsoft Deployment Toolkit 2010 snap-in contains cmdlets used to administer the contents of a deployment share.

You can see that the Microsoft.BDD.PSSnapIn is loaded now like specified in the .psc1 XML file.

:/>  Ярлык. Создание и свойства. Запуск приложений с помощью ярлыка. Как изменить свойства ярлыка Как добавить в свойства ярлыка значение

PowerShell 7

ConfigurationFile

The -ConfigurationFile parameter can be used to specify a .pssc file in which certain session restrictions are configured. More information here.

NoProfileLoadTime

The -NoProfileLoadTime parameter hides the profile load time text from displaying if the load time exceeds 500 miliseconds. For example:

PS C:\Users\HarmVeenstra> pwsh.exe
PowerShell 7.4.1
Loading personal and system profiles took 923ms.
PS C:\Users\HarmVeenstra> pwsh.exe -NoProfileLoadTime
PowerShell 7.4.1
C:\Users\HarmVeenstra>

SettingsFile

The -SettingsFile parameter can be used to override the default powershell.config.json settings. You can find information about the options that you can use here. To use the file, you can run this:

PS C:\Users\HarmVeenstra> pwsh.exe -SettingsFile c:\scripts\settings.json
PowerShell 7.4.1
C:\Users\HarmVeenstra>

Running Built-in PowerShell Commands with Custom Fields

In Filewave when using custom fields for PowerShell scripts the commands are run in a 32-bit environment. Some built-in PowerShell commands require 64-bit to work. In order to run the commands you need to modify the command to launch the correct PowerShell environment. 

get-localgroupmember -group 'Administrators' | select Name

client script powershell.png

Since this is the case you will now need to modify the command choosing to run in 64 bit. To do this you would add “C:\Windows\system32\windowsPowerShell\v1.0\powershell.exe” to the command and run it as a Bat script instead of PowerShell in the Custom Field. I posted the code that would work with the Custom Fields below as well as a screen shot of the correct setup. 

C:\Windows\system32\windowsPowerShell\v1.0\powershell.exe "get-localgroupmember -group 'Administrators' | select Name"
exit 0

client script powershell 2.png

#############################################################################
#If Powershell is running the 32-bit version on a 64-bit machine, we
#need to force powershell to run in 64-bit mode .
#############################################################################
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") { #write-warning "Take me to 64-bit....." if ($myInvocation.Line) { &"$env:WINDIR\system32\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line }else{ &"$env:WINDIR\system32\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args }
exit $lastexitcode
}
# Main script
# Uncomment the next line to prove that we are always in 64bit
#[Environment]::Is64BitProcess
# Your 64bit script here.
#############################################################################
#End
#############################################################################