Working on a corporate laptop with no access to local admin but with the execution policy set to unrestricted, when trying to import the SQLPS cmdlets I receive an error indicating a problem with the path, shown below.
Why is the import-module command looking at my AppData\Local\Temp folder?
If a reinstall of SSMS does not do the trick, try installing the PowerShellTools.msi package of the SQL Server feature pack that corresponds with the latest server you will be working on.
SQL Server 2014
SQL Server 2016
I would also recommend running Get-Module Sqlps | Format-Table -Property ModuleType,Name,Path to check if there are multiple copies of the module and remove them as needed.
Related
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.
I'm coming onto a project that uses PowerShell to script the build. The build makes use of the WebAdministration module to manage the local IIS instance. When I run the build script the following error is thrown when trying to import WebAdministration.
Error: 06/29/2016 17:28:35: At
C:\dev\src\nib-ravendb\build\ConfigureIis.ps1:10 char:1 +
Import-Module WebAdministration + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
[<<==>>] Exception: The specified module 'WebAdministration' was not
loaded because no valid module file was fo und in any module
directory. ERROR: 1
How ever when I run Import-Module WebAdministration at the PowerShell command line the module is imported and I can use features from it. Subsequently running the build script still fails.
I have IIS 7.5 and PowerShell 4
Does anyone have an idea why this import would be failing in the script but not at the command line, and how to fix it?
For servers you need to install the role Management Tools under Web Server (IIS) to use the WebAdministration module. To see if you have the module available use Get-Module -ListAvailable.
For Windows 7 to 10 you will need to install the feature IIS Management Scripts and tools under Internet Information Services >> Web Management Tools.
You could try manually locating the WebAdministration .psd1 file and then import it. Use $env:psmodulepath to help locate where your modules are stored then run:
Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1'
If Server 2008 you could try the following but this may not work on 2012 and upwards:
Add-PSSnapin WebAdministration
Note You will need to run the script with administrator rights to be able to load the WebAdministration module with Import-Module or Add-PSSnapin.
Also check that you have PowerShell's execution Policy set to Unrestricted:
Set-ExecutionPolicy unrestricted
You might want to see this Question.
I had the same situation, i've fixed it installing the Windows Feature Web-Scripting-Tools on W2016 Server:
Add-WindowsFeature Web-Scripting-Tools
In the end there was a problem something, possibly chocolatey?, was truncating $env:PSModulePath to the first entry, this is why the script was working if I typed it in but not in the script.
I found it by logging $env:PSModulePath at different points in the scripts that I was running.
I worked around it by reordering the entries in $env:PSModulePath.
Have a look at #Richard's answer for some other good suggestions.
In my case (Windows 10) I was using Powershell 7 and this simply refused to install the WebAdministration module, despite it being present in Windows Features.
Using a previous version of PS: e.g. Developer PowerShell for VS worked.
We have recently started using SQL Server 2012 SP3 and building the SQL server 2012 using a PowerShell script. There is a requirement in our automation process to run multiple database scripts on a db and I have found Invoke-Sqlcmd very reliable until I found this issue.
When I run Invoke-sqlcmd with a proper set of parameters in PowerShell's debug mode on the system on which the SQL server is installed recently, I don't have problem.
PowershellCommand : Invoke-Sqlcmd -InputFile $sStrJBSPExecRolePath -ServerInstance $sStrSQLName -ErrorAction Stop
But when I execute same query through a PowerShell automation script after rebuilding the same server, I end up getting below error
The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I did research online many suggested to Import SQLPS, etc., so for testing I added the below command in my script
get-pssnapin -Registered
Import-Module “sqlps” -DisableNameChecking**
Even after adding the above into the script, I still end up with same error. But when I run the same script manually it runs perfectly fine. I don't understand what is wrong.
PowerShell automation script - This script installs the .Net Framework 3.5, SQL Server 2012, SQL Server 2012 SP3, and then loads the SMO assembly that I use to change SQL settings such as the Max Memory limit of SQL.
Open up PowerShell as an Administrator and install the sqlserver module by Install-Module sqlserver
After the module has installed, the module commands including the Invoke-sqlcmd should be readily available.
You can check the same using Get-Command -Module sqlserver.
If this module is not readily available, you can Import-Module sqlserver after installing it.
This is not a complete solution, but just a work around which is working for me.
When you execute the query from automation the user which is executing that is not having access to the sqlcmd. Execute you command for the directory where your sqlcmd.exe is present.
Just put
CD "C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn".
to get the location for sqlcmd search the location for SQLCMD.exe in the search box.
if not found, you need to install that where it is missing, but in your case I think it is present, you just need to get the location right.
Also you will need set the path variable for the user executing the automation script or else it will only recognize the sqlcmd, but wont execute that.
$env:Path += ";C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\"
you can get this path from you local user for which it is working by $Env:Path
We have one script server running Windows Server 2012 with PowerShell 4.0 and multiple other servers that run Windows Server 2008R2 with PowerShell 4.0.
What we're trying to do is use the function Get-FSRMQuota from the module FileServerResourceManager found on Server 2012 on the 2008R2 file servers. When in an RDP session on the 2008R2 file server, this can be easily done:
# From PowerShell on the file server:
New-PsSession $ScriptSever
Invoke-Command -Computer $ScriptSever {Import-Module FileServerResourceManager}
Invoke-Command -Computer $ScriptSever {Get-FSRMQuota}
However, all our scripts launch from the script server and are managed there. So we have a job that's initiated on the script server (2012) to run on the 2008R2 servers using PS Remoting. But from within this job it's not possible to import a module from the script server that initiated the job.
So my question, is there a way to import the module FileServerResourceManager from the script server into the remote session on the file server?
I hope I made it clear, as it's a bit difficult to explain. Thank you for your help.
It's not possible. According to https://technet.microsoft.com/en-us/library/jj900651(v=wps.620).aspx, FileServerResourceManager is only available as of Windows 2012.
Windows 7 workstation
POSH 3.0
SS 2012 SP1
Abstract:
Developer 1 cannot run Invoke-SQLCmd
Developer 2 with similar SS Client tools installation can run Invoke-SQLCmd
Developer 1 cannot run Import-Module SQLPS
When I try to run a query from my laptop like the following:
Invoke-sqlcmd -ServerInstance <ServerName> -Query "Select top 10 * from <SomeTable>;"
I get the following error:
Invoke-sqlcmd : The 'Invoke-sqlcmd' command was found in the module 'SQLPS',
but the module could not be loaded. For more information, run 'Import-Module
SQLPS'.
Yet another developer can successfully run the command, and we both have a local
I cannot run Import-Module sqlps on my machine for obvious reasons - ExecutionPolicy is Restricted.
I am not sure why the other developers machine allows the query, while mine does not. Is this a SQL Client install issue?
It is all about Microsoft. They have the best technology but not simply one way and straight path to use their tool. :(
You can use SMO object to Interact with your database. (You can use it with C#/VB.net/PowerShell) :$
You can use SQLPS
You can use Invoke-SQLCmd
Just Run below Script to make sure feature pack installed in your machine and Use SQLPS which is easier to use than other solution (personal preference)
Install-module -Name SqlServer -Scope CurrentUser
# Import the SQL Server Module.
Import-Module Sqlps -DisableNameChecking;
# To check whether the module is installed.
Get-Module -ListAvailable -Name Sqlps;
cd SQLSERVER:\SQL
Maybe you can try the below
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Although I notice those snap-ins are not installed on my local but I am able to use SQLPS but that could be because Execution policy is Unrestricted.
Source : SQL Authority
Update : Download and Install in below order
Microsoft® System CLR Types for Microsoft® SQL Server® 2012
Microsoft® SQL Server® 2012 Shared Management Objects Microsoft®
Windows PowerShell Extensions for Microsoft® SQL Server® 2012
Link - https://www.microsoft.com/en-us/download/details.aspx?id=29065
I found myself in the same situation.
There are two products: SQLPS module and SQLServer Module.
SQLPS is installed automatically when you install an older SQL client.
It can also be extracted from that installation and copied on another host if you cannot install SQL client there (PROD env. for example). You have here the steps in the "details" paragraph
SQLServer Module can be installed via powershell commands but it requires powershell v5 . see here details