Reputation: 23
I'm currently trying to write a script and I'm relatively new to PowerShell:
function RunScript {
# Prompt the user for their username
$username = Read-Host "Enter your username"
# Prompt the user for their password
$password = Read-Host -AsSecureString "Enter your password"
# Prompt the user for the worker ID
$workerId = Read-Host "Enter the ID"
# Create a PSCredential object using the provided username and password
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
# Create a new PSSession using the provided credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "LINK/PowerShell/" -Credential $credential
# Import the PSSession
Import-PSSession $session
# Get the AD user and their group membership, then retrieve the distinguished names of the distribution groups
$groups = Get-ADUser -Identity $workerId | Get-ADPrincipalGroupMembership | Select-Object -ExpandProperty DistinguishedName | Get-DistributionGroup -ErrorAction SilentlyContinue
# Return the output of the last command
return $groups
}
# Run the script and store the output
$output = RunScript
# Output the result
Write-Output $output
The script works well in the ISE, but when I right-click on the file in File Explorer and run it with PowerShell and I input the username, password and workerid, it takes a bit, and then I just see the script outputting tons of users data and then the window closes.
It's like the command is not working correctly.
Upvotes: 1
Views: 263
Reputation: 23
I managed to solve the issue I was experiencing with my PowerShell script. The problem was related to how the script handled sessions and outputs differently in the PowerShell ISE compared to the standard PowerShell console. Here's the revised version of the script that works effectively in both environments:
# Get user credentials
$ExchangeServerURL = "http://your-exchange-server-url/PowerShell/"
$UserCredential = Get-Credential -Message "Enter your credentials"
if ($null -eq $UserCredential) {
Write-Host "No credentials entered. Exiting script."
exit
}
# Establish a session with the Exchange server
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExchangeServerURL -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session -DisableNameChecking | Out-Null
# Main loop for user input and group retrieval
while ($true) {
$userId = Read-Host -Prompt "Enter User ID (or 'exit' to quit)"
if ($userId -eq 'exit') { break }
if ([string]::IsNullOrWhiteSpace($userId)) {
Write-Host "Invalid input. Please enter a User ID." -ForegroundColor Red
continue
}
try {
$groups = Get-ADUser -Identity $userId -ErrorAction Stop |
Get-ADPrincipalGroupMembership |
Select-Object -Property Name, GroupCategory, GroupScope |
Format-Table -AutoSize
if ($groups) {
Write-Host "Groups for user $userId:" -ForegroundColor Green
$groups
} else {
Write-Host "No groups found for user $userId." -ForegroundColor Yellow
}
} catch {
Write-Host "Error fetching groups for user $userId. Please try again." -ForegroundColor Red
}
}
# Close the session
Remove-PSSession $Session
This revised script addresses the original issues by:
This version works reliably both in PowerShell ISE and when executed directly from the File Explorer. Hopefully, this solution can help others facing similar issues.
Upvotes: -1
Reputation: 439672
Executing a PowerShell script from File Explorer, via its shortcut menu, unfortunately does not keep the console window that the script runs in open.
Thus, in order to ensure that you at least see the output from scripts executed this way, you have two options:
For any given script, place something like Read-Host 'Press Enter to exit.'
at the end of the script, to prevent immediate closing of the window on exit.
More fundamentally, you can modify the definition of the shortcut-menu command in the registry to:
either: automatically execute this Read-Host
call after every script invocation
or: keep the console window open until explicitly closed, by entering a persistent, interactive session.
Both approaches are shown below.
Run with PowerShell
File Explorer shortcut-menu command to keep the window open:The following applies to:
Windows 11
Windows PowerShell, the legacy, Windows-only PowerShell edition (as opposed to the install-on-demand, cross-platform PowerShell (Core) 7+ edition).
Instructions:
Step 1: Define the new command line.
Note:
-File
or -Command
with -NoProfile
.If you want to enter an interactive session after executing a script, place the -NoExit
switch of powershell.exe
, the Windows PowerShell CLI, before the -File
parameter (-NoLogo
is also specified to suppress the copyright message):
$newCmdLine = '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoExit -File "%1"'
If you'd rather just wait for the user to press Enter before the window closes, use the -Command
CLI parameter and append the Read-Host
command:
$newCmdLine = '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "& \"%1\"; Read-Host ''Press Enter to exit.''"'
Step 2: Write the new command line to the registry:
To modify the definition system-wide (for all users), run the following, which requires elevation (running as admin):
#requires -RunAsAdministrator
Set-ItemProperty registry::HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Windows.PowerShell.Run\Command '(Default)' $newCmdLine
To modify the definition for the current user only - which doesn't require elevation - run:
$regKey = New-Item -Force registry::HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.ps1\Shell\Windows.PowerShell.Run\Command
Set-ItemProperty "registry::$regKey" '(Default)' $newCmdLine
Upvotes: 0