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.
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.