How do you avoid all of the unwanted console output when doing Import-PSSession? I'm writing a script to monitor some stuff in Exchange and it needs to drop into our monitoring system and only produce very specific output, but whenever I import my Exchange session it produce
WARNING: Some imported command names include unapproved verbs which
might make them less discoverable. Use the Verbose parameter for more
detail or type Get-Verb to see the list of approved verbs.
I've tried:
$Session=(Import-PSSession(New-PSSession -ConfigurationName Microsoft.Exchange \
-ConnectionUri http://CasServer/PowerShell/ -Authentication Kerberos \
-Credential $Cred -AllowClobber -WarningAction:SilentlyContinue)
It still displays the unwanted text. I've also tried -ErrorAction:SilentlyContinue; doesn't work.
You can use the following switch to suppress the warning, if specifying all the cmdlets you want to use isn't feasible:
-DisableNameChecking
Example:
Import-PSSession $session -DisableNameChecking
I think better solution is to read the output of the Import-PSSession into variable such as:
$output = Import-PSSession $session -AllowClobber
Then you can read the $output and see if it is a warning, error etc.
But don't use -WarningAction:SilentlyContinue or -ErrorAction:SilentlyContinue because you'll never see if it is oK or not
You are setting -WarningAction on Import-PSSession. Warning you get smells like Import-Module (that Import-PSSession calls behind the scenes).
You may change $WarningPreference global variable to SilentlyContinue for the life of your script. That would silent the warning you get.
you can try pipe to | out null
Another option:
Import-PSSession $session 3> $null
Related
Having issues with remove-mailboxPermission and Add-MailboxPermission. I receive the following error:
The Command Get-Mailbox works however the rest does not (note: I've edited out our DNS)
#PowerShell script to add access to an email and not map
Set-ExecutionPolicy RemoteSigned
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Get-Mailbox davidb#aaa.com
Remove-MailboxPermission -Identity davidb#aaa.com -User AshleyD#aaa.com -AccessRights FullAccess
Add-MailboxPermission -Identity davidb#aaa.com -User AshleyD#aaa.com -AccessRights FullAccess -AutoMapping:$false
Remove-PSSession $Session
The error "A term _____ is not recognized as the name of a cmdlet..." can be misleading. If your syntax is correct it usually means that you don't have sufficient permission to run that commandlet.
You can use this guide to find out which specific permission you need to run each cmdlet:
https://learn.microsoft.com/en-us/powershell/exchange/exchange-server/find-exchange-cmdlet-permissions?view=exchange-ps
You may be wondering "why doesn't it just tell me that I don't have permission?" It makes a little more sense when you understand why you get this error. Remember that your session can't see(for lack of a better term) parameters or commandlets you don't have permissions for. So depending on what you are trying to do PowerShell may tell you "thats not a valid command" or "thats not a valid parameter", when in fact those are valid commmands and parameters, your session just can't see them if you don't have access to run it. This will also happen if you are connected to a wrong URI in your O365 PowerShell session(e.g. the compliance uri instead of the outlook uri)
EDIT: This site says you need to be a member of the "Organization Management" group in order to run these cmdlets.
I'm using powershell, I just use Get-MessageTrackingLog after login/Authentication
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $UserCredential -Authentication Basic -AllowRedirection/ .
But I'm getting the following error:
Get-MessageTrackingLog not recognized as the name of a cmdlet
I want to search all emails in a mail box.
The error message states that the Powershell environment does not have definition for function and yet cmdlet Get-MessageTrackingLog is not recognized.
Cmdlet Get-MessageTrackingLog is part of exchange Powershell module.
In order to use any function from this module you would need to install and then load it.
I'm new to Powershell scripting and I'm working on a user management Powershell script, but I have run into a strange error.
The following code works when I run it from the shell, but not when it is run from a script:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
When I run it in a script with a Param() block on the first line, it fails with the following error:
Import-PSSession: Cannot bind argument to parameter 'Path' becuase it is an empty string.
I can get the Import-PSSession to work if I remove my Param() block, but I'm not sure how to accept command-line arguments for my script otherwise. How can I keep the Param() block (or more generally, accept command-line arguments) and still be able to import the PS session?
I'm using Powershell v2 on a Windows 2008 R2 server trying to connect to a 2010 Exchange server.
Update:
I have a script named ManageUser.ps1 that I run from a Powershell prompt like
.\ManageUser.ps1 -disableUser -username someuser
The script starts like this:
Param(
[switch]$disableUser,
[string]$username
)
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
#more stuff past here...
It is failing on the Import-PSSession command. But, if I remove the whole Param(...), the session import works. I hope this is enough for you to understand it!
Your script works fine on it's own and also when called from within another script:
& 'C:\Scripts\ManageUser.ps1' -disableUser -username "foo"
Get-Mailbox -resultsize 5 | ConvertTo-Csv -NoTypeInformation | Out-File C:\Scripts\mytest.csv
Subsequent runs will get the import error/warning due to not having -allowclobber as mentioned by #SamuelWarren...
In your case (at least when initially writing the question long ago) the error was due to another variable that you haven't mentioned here. It's likely you fixed that after the first run, and then subsequent tests were showing the AllowClobber error.
Also worth noting (for anybody who comes across this in the future) to check the path of the file you are calling. Just because you try to use a relative path .\myfile.ps1 doesn't mean that PowerShell is currently looking in the right directory.
Check: $psscriptroot
Or Split-Path -Path $($global:MyInvocation.MyCommand.Path)
I have created a PowerShell Module. Each function in the module has it's own script file so I can keep track of them.
I have a problem with the function below:
function Connect-O365
{
$global:o365Cred = Get-Credential
$global:o365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell -Credential $global:o365Cred -Authentication Basic -AllowRedirection -Verbose
Import-PSSession $global:o365Session -Verbose -AllowClobber
Connect-MsolService –Credential $global:o365Cred -Verbose
Get-Mailbox # SEE BELOW
}
When I call the function it goes through all the motions and appears to run correctly. No errors are shown. However when I call teh get-mailbox function I get an error that the 'command or cmdlet does not exist'.
If I execute each line of code on the command line it works fine. If I add the function to my PowerShell profile it also works fine. I also added the Get-Mailbox line to the end of it (as next to the comment # SEE BELOW) and it displays the mailboxes.
What it looks like to me is something to do with scope. As soon as this is put into a module function it stops working as it should. As soon as it runs and exits the commands that I'm needing disappear.
I have tried making the function global but that doesn't work either.
Does anybody have any suggestions?
Anyone would help me this? I've tried this solution also Import-PSSession : Proxy creation has been skipped for '%' command, because PowerShell couldn't verify its name as safe but still it doesn't work for me.
It says that when i enter Import-PSSession $Session
WARNING:Proxy creation has been skipped for the following command: 'TabExpansion', because it would shadow an existing local command. Use the AllowClobber parameter if you want to shadow existing local commands.
Then i try the Import-PSSession $Session -AllowClobber. Still it doesn't work. The error says:
Import-Module : There were errors in loading the format data file:
Microsoft.PowerShell, , C:\User\User\AppData\Local\Temp\temp_3b3f42b5-ea81- .....