2

I'm trying to use SqlPowershell/SqlPS (Import-Module sqlps) to connect to a server on my local network. I followed the instructions here to create a function that predefines a server/database and the login and prompts each time for the password:

function sqldrive
{
    param( [string]$name, [string]$login = "<myLogin>", [string]$root = "SQLSERVER:\SQL\<serverName>\<databaseName>" )
    $pwd = read-host -AsSecureString -Prompt "Password"
    $cred = new-object System.Management.Automation.PSCredential -argumentlist $login,$pwd
    New-PSDrive $name -PSProvider SqlServer -Root $root -Credential $cred -Scope 1
}

However, after running sqldrive <someName> and entering my password, it fails to connect, giving two messages:

WARNING: Could not obtain SQL Server Service information. An attempt to connect to WMI on '<serverName>' failed with
the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

and

New-PSDrive : SQL Server PowerShell provider error: Path SQLSERVER:\SQL\<serverName>\<databaseName> does not exist. Please
specify a valid path.
At line:6 char:5
+     New-PSDrive $name -PSProvider SqlServer -Root $root -Credential $ ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (SQLSERVER:\SQL\<serverName>\<databaseName>:String) [New-PSDrive], GenericProviderException
    + FullyQualifiedErrorId : PathDoesNotExist,Microsoft.PowerShell.Commands.NewPSDriveCommand

Can anyone spot where I'm going wrong? I can work with the same server just fine using those credentials via SSMS.

Benjin
  • 2,264
  • 2
  • 25
  • 50
  • Since it's a WMI error, have you tried enabling WINRM on the remote system? – Eris Mar 02 '16 at 19:53
  • How is WMI connected to WINRM? – Mike Shepard Mar 02 '16 at 21:01
  • On the target machine, I ran `winrm quickconfig`. It said WinRM was already running, but the firewall wasn't configured. It asked if I wanted it to add the firewall exception and I said yes. I'm still seeing the same result as the original post. – Benjin Mar 03 '16 at 00:30
  • Any progress on this? – unlikely Oct 01 '17 at 10:44
  • @unlikely Yup, sorry for not updating the thread. Perhaps not what you're looking for, but I eventually discovered that I could just construct the database object and pass that to `-InputObject` instead of setting up a PS drive. The easiest way to do that is `$db = Get-SqlDatabase -ConnectionString $myConnectionString` – Benjin Oct 23 '17 at 23:01

0 Answers0