PowerShell SqlModule corrupted - powershell

I have installed SqlServer moduled with Install-Module -Name SqlServer -Repository PSGallery -Verbose command. PS says that it was successfuly.
But when I'm runneng import-module I only see 3 items imported from this module:
PS >import-module sqlserver -erroraction stop -verbose
VERBOSE: Loading module from path 'C:\Program
Files\WindowsPowerShell\Modules\sqlserver\21.0.17240\sqlserver.psd1'.
VERBOSE: Populating RepositorySourceLocation property for module sqlserver.
VERBOSE: Loading module from path 'C:\Program
Files\WindowsPowerShell\Modules\sqlserver\21.0.17240\SqlServer.psm1'.
VERBOSE: Exporting function 'SQLSERVER:'.
VERBOSE: Exporting alias 'Encode-SqlName'.
VERBOSE: Exporting alias 'Decode-SqlName'.
VERBOSE: Importing function 'SQLSERVER:'.
VERBOSE: Importing alias 'Decode-SqlName'.
VERBOSE: Importing alias 'Encode-SqlName'.
Question: how to get all the commands (like Invoike-Sqlcmd and other) from SqlServer working?
PS Version
Name Value
---- -----
PSVersion 5.1.16299.251
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.251
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
UPDATE:
Running Get-Command -Module SqlServer | Measure gives even after force reinstall with Install-Module SqlServer -Force:
PS H:\> Get-Command -Module SqlServer | Measure
Count : 3
Average :
Sum :
Maximum :
Minimum :
Property :
Checked event log. Found bunch of Information records, when importing the SqlServer module like this:
Context Information:
DetailSequence=1
DetailTotal=1
SequenceNumber=17
UserId=DOMAIN/User
HostName=ConsoleHost
HostVersion=5.1.16299.251
HostId=GUID
HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
EngineVersion=5.1.16299.251
RunspaceId=GUID
PipelineId=6
ScriptName=C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.0.17240\SqlServer.psm1
CommandLine= Add-Type -Path $binaryPath
Details:
CommandInvocation(Add-Type): "Add-Type"
ParameterBinding(Add-Type): name="Path"; value="C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.0.17240\Microsoft.SqlServer.Dmf.dll"

Hmm. Okay do this...
Find all your various module folders -- you can use the following to do this:
$env:PSModulePath.Split(';')
Removing the "sqlserver" folder from each directory, if it exists.
Re-import the module.
Install-Module -Name SqlServer -AllowClobber
Steps-1 and -2 make sure there are no mangled SqlServer module installations lurking anywhere. Step-3 gives us a fresh pull of the module.
The only thing I can think of. Maybe there is a mangled module installed somewhere. It isn't getting overwritten during re-install, but it's accidentally getting loaded.

Related

Type not found in PowerShell session: "Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage"

Below is example variable which once entered into PowerShell Core reports that the type was not found:
[Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage] $test = $null
InvalidOperation: Unable to find type
[Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage].
However once you run the following code:
Get-AppxPackage -PackageTypeFilter Main
Then the previous variable declaration will be just fine (no error):
[Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage] $test = $null
How do I make this type get recognized by PowerShell session without running Get-AppxPackage?
I tried with using namespace Microsoft.Windows and few other namespaces but it doesn't work.
Environment data:
$PSVersionTable
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.19041
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
I figured out, at the top of your script add this line:
Add-Type -AssemblyName Microsoft.Windows.Appx.PackageManager.Commands
Now we can use all the types from that assembly:
[Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage] $test = $null
No error.
Reference

Can't Get Screen Resolutions from Remote Computers via PowerShell Remoting

I'm trying to get correct screen resolutions from remote computers using Invoke-Command and a PSSession but both methods report one resolution of 1024 x 768. There are two screens, 1920 x 1080 and 1280 x 720. This is not a DPI thing.
Executing the code below (remotely) outputs the below snippet. Executing interactively, the correct resolutions are reported. All the other working methods posted online have the same behavoir.
Output:
PS > Add-Type -AssemblyName System.Windows.Forms
PS > [System.Windows.Forms.Screen]::AllScreens
BitsPerPixel : 0
Bounds : {X=0,Y=0,Width=1024,Height=768}
DeviceName : WinDisc
Primary : True
WorkingArea : {X=0,Y=0,Width=1024,Height=768}
It's surprising nothing in CIM can get these details from MULTIPLE monitors remotely, that I have found. Maybe time for a feature request to beef up some CIM cmdlets in 7.
I guess a workaround could be creating a Scheduled task on all target computers that runs a script locally to output the info to a local file, then use remoting to get the file or data.
If anyone has overcome this, your feedback would be greatly appreciated amongst the rest of us with this roadblock.
PowerShell 7:
Name Value
---- -----
PSVersion 7.0.1
PSEdition Core
GitCommitId 7.0.1
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Windows PowerShell:
Name Value
---- -----
PSVersion 5.1.18362.752
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.752
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Windows 10 Pro
I am not sure if this issue was resolved or not, but I am not able to find any way to get resolution of remote PC via powershell (i.e. using non-interactive session). Hence came up with the logic to get this via registry values :
Note :-
This script is tested with Windows 10 only
This will require remote user to have read access to registry value in variable "$videoRegistryPath"
$videoRegistryPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\'
$all_monitor_array_X=#()
$all_monitor_array_Y=#()
$screen_cnt= (Get-CimInstance -Namespace root\wmi -ClassName wmimonitorid -ErrorAction SilentlyContinue | measure )
$NumberOfScreens= $screen_cnt.Count
$allVideoGraphics = Get-ChildItem -Path ($videoRegistryPath).Replace('HKEY_LOCAL_MACHINE','HKLM:')
foreach ($instance in $allVideoGraphics ) {
$allVideoGraphicsChild= Get-ChildItem -Path ($instance.Name).Replace('HKEY_LOCAL_MACHINE','HKLM:')
if ($allVideoGraphicsChild.Property.Contains('PrimSurfSize.cx')) {
$allVideoGraphicsChildProperty = Get-ItemProperty -Path ($allVideoGraphicsChild.Name).Replace('HKEY_LOCAL_MACHINE','HKLM:')
$Screen_X= $allVideoGraphicsChildProperty.'PrimSurfSize.cx'
$Screen_Y=$allVideoGraphicsChildProperty.'PrimSurfSize.cy'
$all_monitor_array_X += $Screen_X
$all_monitor_array_Y += $Screen_Y
}
}
for ($i=0;$i -le ($NumberOfScreens-1);$i++){
[PSCustomObject]#{
Values = $all_monitor_array_X[$i];
Instance = "Monitor_"+($i)+"_x_coordinate";
}
[PSCustomObject]#{
Values = $all_monitor_array_Y[$i];
Instance = "Monitor_"+($i)+"_y_coordinate";
}
}
Hope this will help in this specific requirement to get resolution of remote computer using non-interactive session.

powershell update-help fails

I just started to learn PowerShell from the Microsoft virtual academy and I was running one of the commands indicated. while on their end it worked mine did not. I did look around for a solution to this issue. I just don't know what went wrong. any tips will helpful for this new PowerShell learner.
PS C:\Windows\system32> Update-Help -Force
Update-Help : Failed to update Help for the module(s) 'WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US. Make sure the
HelpInfoUri property in the module manifest is valid or check your network connection and then try the command again.
At line:1 char:1
+ Update-Help -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17134.228
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.228
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Agreed, that this should be a SuperUser post, but since it is here.
This sort of error common and in most cases expected.
Not all the help files, update as expected for various reasons, most of the time its do to the update link associated. As shown in your error message.
Many module either have no online updateable help or the URL has been removed.
These sorts of error can be safely ignored. They do not impact PS functionality or use.
Get-Module -ListAvailable | Where HelpInfoUri | Update-Help
Or if you want to see all the message going back and forth with this, do...
Update-Help -Force -Verbose -ErrorAction SilentlyContinue
# Results
VERBOSE: Resolving URI: "http://go.microsoft.com/fwlink/?linkid=390758"
VERBOSE: Your connection has been redirected to the following URI: "http://download.microsoft.com/download/0/1/C/01CCC594-2F13-40E8-98FE-185486228BF4/"
VERBOSE: Performing the operation "Update-Help" on target "CimCmdlets, Current Version: 5.0.0.0, Available Version: 5.0.0.0, UICulture: en-US".
If you want to see the full error message in a more human readable for, do this...
Update-Help -Force -Ea 0 -Ev ErrMsgDetail
$ErrorMsgDetail.Exception
Failed to update Help for the module(s) 'AnyBox' with UI culture(s) {en-US} : Unable to connect to Help content. The server on which Help content is stored might not be available.
Verify that the server is available, or wait until the server is back online, and then try the command again.
Failed to update Help for the module(s) 'HostNetworkingService, WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US.
Make sure the HelpInfoUri property in the module manifest is valid or check your network connection and then try the command again.
Actually the problem might also be related to privileges. At least it was for me. On a default Windows 10 install, Update-Help would not work at all, nor did all the other versions with -Force or -ErrorAction.
It turned out, also according to the official online help, you need to be administrator to update the help for powershell <6.0 (which Windows 10 defaults to PS5.1). Starting a new PowerShell with admin privileges immediately removed the problems and I could also view the help as a non-admin.
TL;DR:
Open Powershell, paste the command below and run Update-Help and it should download all AVAILABLE help files:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
It will a give a red warning for the modules that did not have any help files like:
update-help : Failed to update Help for the module(s) 'ConfigDefender, ConfigDefenderPerformance, PSReadline,
WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US.
Make sure the HelpInfoUri property in the module manifest is valid or check your network connection and then try the
command again.
At line:1 char:1
+ update-help
+ ~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand
Long story:
I was trying to find a module and install it:
Find-Module -Name AudioDeviceCmdlets
But it needs NuGet so it prompts to install it but it failed for me:
NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'$HOME\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import
the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
WARNING: Unable to download the list of available providers. Check your internet connection.
So I googled for a solution and found out that I need to paste this on Powershell:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
And the NuGet installed just fine, so I remember the issue I had with Update-Help, so I tried it and it worked, and now when I do for example Get-Help Get-Item the help is shown fully.
From Niels Weistra on answers.microsoft:
The solution mentioned above is a workaround, to solve your issue permanently
Open Powershell and check for supported protocols by using [Net.ServicePointManager]::SecurityProtocol
Run the following 2 cmdlets to set .NET Framework strong cryptography registry keys:
Set strong cryptography on 64 bit .Net Framework (version 4 and above):
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set strong cryptography on 32 bit .Net Framework (version 4 and above):
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Restart Powershell and check again for supported protocol by using [Net.ServicePointManager]::SecurityProtocol
Source:
https://answers.microsoft.com/en-us/windows/forum/all/trying-to-install-program-using-powershell-and/4c3ac2b2-ebd4-4b2a-a673-e283827da143
Install NuGet via PowerShell script
I am new to Powershell and found this fixed my problem.
reference link
Update-Help -Verbose -Force -ErrorAction SilentlyContinue

PowershellGet missing from Windows 10 ver 1709

I was attempting to install the lastest VMWare PowerCLI, and I get an error: Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet, function...
Some digging shows that I don't have PowershellGet installed, and all of the documentation/downloads either say "Out of the box", or run install-module to install PowershellGet, as there's not a download available for Windows 10.
I don't have a PowershellGet folder in C:\Program Files\WindowsPowerShell\Modules, but I do have PackageManagement.
Where do I find a download for PowershellGet?
Windows 10 version 1709
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.16299.248
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.248
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Install-Module is built in to PowerShell in Windows 10 so it should be present.
This can occur if the Language Mode is not Full Language. I've experienced this with Constrained Language Mode, but haven't tested the others.
Language Mode is controlled by the variable __PSLockdownPolicy. Constrained Language Mode was designed to limit the functionality available to non-administrator users when used with other products like AppLocker or Device Guard User Mode Code Integrity (UMCI).
More information:
https://blogs.msdn.microsoft.com/powershell/2017/11/02/powershell-constrained-language-mode/
https://blogs.technet.microsoft.com/kfalde/2017/01/20/pslockdownpolicy-and-powershell-constrained-language-mode/
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes
Get-Module -ListAvailable Microsoft*| Select name | Out-String | % {$_ -replace "Name"}|% {$_ -replace "-"}|% {$_ -replace " "} |out-file -filepath .\ModulesbyName_Microsoft.txt
Wait-Event -Timeout 5
Get-Content -Path .ModulesbyName_Microsoft.txt | Import-Module -verbose
Check out the PowerShellGallery and you should find everything you need to install PowershellGet. Also its a nice GUI browser for modules.

Powershell v3.0 pipe issue

I'm having trouble with this command:
gc .\domains.txt | Get-ADDomain
As the name implies, domains.txt contains a list of Active Directory to query (all domains are in the same forest).
If I run it on my Windows 8 machine everything works fine and I get the expected results, instead on a Windows 2008 R2 SP1 member server (not a DC) with WMF 3.0 I get result only from the first domain in the list and for the others:
Get-ADDomain : A referral was returned from the server
If I query a domain in the list with:
Get-ADDomain <Domain name here>
it works fine.
My Workstation
Microsoft Windows 8 Enterprise (6.2.9200) x64
PS D:\Tools\Powershell> $PSVersionTable
Name Value
---- -----
PSVersion 3.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18010
BuildVersion 6.2.9200.16384
PSCompatibleVersions {1.0, 2.0, 3.0}
PSRemotingProtocolVersion 2.2
Server
Microsoft Windows Server 2008 R2 Standard SP1 (6.1.7601) x64
PS C:\Tools\Powershell> $PSVersionTable
Name Value
---- -----
WSManStackVersion 3.0
PSCompatibleVersions {1.0, 2.0, 3.0}
SerializationVersion 1.1.0.1
BuildVersion 6.2.9200.16398
PSVersion 3.0
CLRVersion 4.0.30319.269
PSRemotingProtocolVersion 2.2
Update
If i run on the server:
gc .\domains.txt | %{ Get-ADDomain $_ }
it runs fine
TRACE
trace-command -Name ParameterBinding { "DOMAIN_1","DOMAIN_2" | Get-ADDomain } -PSHost
Server: http://pastebin.com/sRVJHaCU
Workstation: http://pastebin.com/kj3JV6nV
Thanks in advance
I found an article that may help.
http://technet.microsoft.com/en-us/library/ee617224.aspx
From the look of your script you are providing the server using the text file. Is it possible the problem is the Windows 2008 server you are running the PowerShell script on is not in the same domain or the user you are logged in as does not have access to the domains where the other servers are members?
snippet from the above article:
-If the Server parameter is specified and the Credential parameter is not specified:
--The domain is set to the domain of the specified server and the cmdlet checks to make sure that the server is in the domain of the
LocalComputer or LoggedOnUser. Then the credentials of the current
logged on user are used to get the domain. An error is returned when
the server is not in the domain of the LocalComputer or LoggedOnUser.
You might try adding the additional parameters for the Get-ADDomain commandlet such as -Identity, -AuthType, and -Credential
Get-ADDomain [-Identity] [-AuthType { |
}] [-Credential ] [-Server ]
[]
Powershell v3.0 pipe issue
I just tried to run the cmdlet 'gc .\text.txt | Get-ADDomain' from a virtual Server 2008 R2 box that I have. I built a text file in the following format:
Domain1
Domain2
Domain3
One thing to be sure of is that each domain is on it's own line in the text file. I can understand why the one syntax worked when you piped the STDOUT to:
%{ Get-ADDomain $_}
because you are looping through all the information contained in the text file and only have the cmdlet work on a single value at a time. Unfortunately I don't have the RSAT package on my Win 8 desktop, so I can't test from my desktop. Hopefully this helps a little bit.