How to prevent Powershell from printing warnings on the output? - powershell

I am executing a PS script to display few things from Azure. But, for some cmdlets the warnings gets written to the output.
like this:
WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell i n an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care th at none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide.
I have used the -ErrorAction SilentlyContinue which will prevent the errors from printing. But is there a way to avoid warnings in the output while executing the command.
P.S: I am well aware of how to get rid of the above warning. Just curious if there is any way we can keep the warnings from displaying.

You're trying to use -ErrorAction on a Warning, which is not an Error.
To silence Warnings, you need to use -WarningAction SilentlyContinue

Related

PowerShell module installed manually. cmdlets found with help but not recognized when executed

I have a machine i would like to install a module on, specifically SwisPowerShell for working with SolarWinds. Internet is disabled on it so i cant use install-module, so i manually downloaded the .nupkg from another machine and went through the steps of unpacking the files into the correct folders. https://learn.microsoft.com/en-us/powershell/scripting/gallery/how-to/working-with-packages/manual-download?view=powershell-7.2. all the .dll files and the windows powershell datafile i have tried in both directories "C:\Program Files\WindowsPowerShell\Modules\SwisPowerShell\3.1.0.343" and "C:\Users\username\Documents\WindowsPowerShell\Modules\SwisPowerShell\3.1.0.343". Importing the module with "import-module -name SwisPowerShell -Global -force -Verbose" shows that everything loads correctly, but then running "get-module SwisPowerShell -Verbose" returns nothing. even loading each dll individually with "add-type -path "C:...\somefile.dll" shows nothing. running "get-module -ListAvailable" shows the module in both (or either) directories as they should be. using "get-help cmdletname" and "get-command cmdletname" shows the correct cmdlet definitions. trying to run any of the cmdlets returns "The term 'connect-swis' is not recognized as the name of a cmdlet, function... yada yadda" for all of the cmdlets. its like powershell sees the files but refuses to use them. Am i missing a step that tells powershell that this is a valid module to use? could it be blocked somehow? what extra steps does install-module do that a manual install does not that i could try?
Try running process monitor while installing on another machine. You might find out it's doing some registry changes too.

Not able to get New-F5Item cmdlet

I am creating a pool in F5.
i came across the below link which gives a function of creating a Pool.
(https://www.powershellgallery.com/packages/f5-ltm/1.4.154/Content/Public%5CNew-Pool.ps1)
All the things are fine. i have installed F5-LTM module for powershell also.
Install-Module -Name F5-LTM -RequiredVersion 1.4.280
I am not able to see the "New-F5Item" cmdlts.
Can you please let me know about this cmdlet.
Thank you
If you look at the file list in PSGallery, you'll notice that the package contains the script file Private\New-F5Item.ps1, indicating that New-F5Item is probably not exported by the module.
When a command is not exported, it can only be discovered/called from other functions in the same module - which is why you don't see it when doing Get-Command or similar.

Is it possible to delete or overwrite cmdlets?

I'm working with DNS resource records in Powershell 5 using code that I inherited from the guy who was trying to do this before me. The cmdlet I am trying to use is Add-DnsServerResourceRecordA.
Part of his code has import-module certain folder\PowerShell\Modules\DnsServer. The weird thing is, it seems like as I was trying bits and pieces of the code earlier, I was able to use the add-DNSblah cmdlet. Now, after It ried running the whole script including the import-module, Powershell is saying that the cmdlet does not exist natively, and when I import the module and run it it is giving me Add-DnsServerResourceRecordA: Invalid Class.
It is my understanding that Add-DnsServerResourceRecordA should be included in my normal Powershell 5.0. Could that Import-Module have permanently damaged PS somehow? Why else would the cmdlet not show up, even in a Get-Command "dns"?
I'm pretty sure you will need the Remote Server Administration Tools (RSAT) installed to have these cmdlets available on a non-server Windows OS.
You can download them from this page: https://www.microsoft.com/en-gb/download/details.aspx?id=45520.
Not really sure why the Import-Module does not fail if the DNSServer module is not present on the system.
If RSAT are already installed, you can try to reinstall them.

Suppressing VERBOSE for Import-Module

I'm importing Carbon into my PowerShell script; however when running my script with -Verbose, Carbon also outputs a lot of VERBOSE statements.
Is it possible to Import-Module silently such that I can ignore the verbose statements in the imported module and leave just my own?
Try Import-Module Carbon -Verbose:$false
I could not get the solutions above to work with all modules (I'm using Powershell 4.0). This is the solution I ended up using and so far it has worked with every module I've used:
At the top of my script file I have this, to make the -Verbose work for the script (the script has no parameters):
[CmdletBinding()]
Param()
Then when I'm ready to import the modules, I do this:
$SaveVerbosePreference = $global:VerbosePreference;
$global:VerbosePreference = 'SilentlyContinue';
Import-module "Whatever";
$global:VerbosePreference = $SaveVerbosePreference;
Then I just call the script like so:
PowerShell -file something.ps1 -Verbose
Import-Module Carbon -Verbose:$false | Out-Null
I think a better solution than the one which is marked here is to redirect the verbose output to a different stream. This way you can print the output if you need it and it doesn't get swollen for ever:
Import-Module Carbon 4>&5
This redirects the verbose stream (4) to the debug stream (5). When you run your script with the Verbose switch, it will not output the verbose lines from Import-Module, but you can bring it back by running your script with the -Debug switch.
As Carbon seems to be a script module, can you try to set the $script:VerbosePreference (or just $VerbosePreference) to 'SilentlyContinue' inside the module itself carbon.psm1. The module scope should do the trick.
First contribution, I hope this helps.
ipmo $dir\$i 3>$null
ipmo: Short-hand/alias for Import-Module
3>$null: Redirect warnings messages to null
Edit:
I wanted to share a table I found at work while I was looking for the best way to help explain this... But I cant find it now. However, the quick an dirty is you may have already noticed that ipmo doesn't act like the rest of those PWshell cmdlet. It has a total of 4 streams in the bg.
I don't remember 1 and 2. 3 is warning. 4 though, 4 is not necessarily error, but it is error at the same time? I don't remember the exact wording. It is something that bypasses errors.
If I do find that award winning table again I'll be sure to share the blessing.

PowerShell on SCOM fails to import module

I have a problem I cannot solve without help )
I have SCOM in first PC, and I have SCOM agent in second. When my class discoveries in agent PC, it must run PowerShell script. This script contains command:
Import-Module FailoverClusters
but this command fails with exception:
The following error occurred while loading the extended type data file:
Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FailoverClusters\FailoverClusters.Types.ps1xml : File skipped because it was already present from "Microsoft.PowerShell".
I dont know what to do.
As this blog post points out, you can ignore extended type data errors when loading modules. It's telling you that the type is already loaded and it can't load it a second time.
WORKAROUND:
I found that with SCOM 2007 R2 (haven't tested this on SCOM 2012), powershell fails to import FailoverClusters module. I tried the suggestion to skip the error. Skipping works the very first time the agent executes the script. After that, subsequent executions of the script fail to have the Get-Cluster cmdlet available. Whenever you restart the agent, it skips the error and the cmdlet is available, but again subsequent executions fail to load the cmdlet.
Elevated permissions and unrestricted script execution didn't help the issue.
Restarting the agent regularly is such a sledge hammer. I did not entertain it.
However I did find that if I used a light-weight script that spawns a new powershell instance and executes my main code (file saved on disk or generate the script on disk on the fly), the fresh powershell instance loaded the module successfully every time and the cmdlet was always available.
I know there are concerns that spawning one instance from another (like vbscript spawing powershell) has perf issues. But in my case, I was able to have the agent call my powershell wrapper, generate a 500 line script on the fly (using streamwriter for perf), and then spawn it in a fresh powershell form the wrapper. It all executed in about 6 seconds, which included querying Get-ClusterResources.
I'm guessing this is a bug in the SCOM agent...
Powershell Script (GPO_Discovery.ps1) called WriteErrorLine method to output the following data:
Import-Module : The following error occurred while loading the extended type data file:
Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ActiveDirectory\ActiveDirectory.Types.ps1xml : File skipped because it was already present from "Microsoft.PowerShell".
As it can be seen, I have the same problem on trying to run :
Import-Module ActiveDirectory
Import-Module GroupPolicy
So I tried:
Import-Module -Name ActiveDirectory -OutVariable $outAD -ErrorAction SilentlyContinue
Import-Module -Name GroupPolicy -OutVariable $outGP -ErrorAction SilentlyContinue
Neither of these have helped. I am about to attempt to remove the Import CMD-Let completely and try again. I will post if that works for me.