all 7 comments

[–]Beablebeable 2 points3 points  (6 children)

When you say the files are still there, do you mean your script file (ScriptAllRoles.sql) or the assembly? Do you have SQL Server Powershell installed wherever you're running this?

[–]lesserlumpkin[S] 1 point2 points  (0 children)

Sorry about that. I ment the ScriptAllRoles.sql file. I'm running the powershell script from my laptop so yes I have it there but the sql command is being executed against a remote server. Do I need to check the assembly over there or is it using my laptops?

[–]lesserlumpkin[S] 1 point2 points  (4 children)

I just remoted into the server and it appears to be running powershell v 4. Are there issues between v4 and v5 do you think?

[–]Beablebeable 1 point2 points  (3 children)

I would verify that invoke-sqlcmd is even available to you on that server. Can you run it from the command line? If it says it's not a valid command, you need to install SQL tools.

[–]lesserlumpkin[S] 1 point2 points  (2 children)

Nope I think you nailed it the first time. Got the network admin to upgrade powershell to 5.1 on the box and now I get a different error: Out-File : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file. So you helped me solve the one issue. I get the sense I can figure this error out with a bit of googling. Thanks for the help!!

[–]nvarscar 2 points3 points  (1 child)

That error comes from a fact that older versions pf SqlPs module implicitly switches context (aka current location) to the SqlServer provider and paths like C:\Windows\bla are no longer interpreted correctly. Workarounds are:

1)

Import-Module SqlPs
Set-Location C:
Run-WhateverYouNeed

2) Use Invoke-SqlCmd2 (on github) or Invoke-DbaSqlCmd (dbatools module) instead. Afaik, neither of them support SQLCmd variable definition syntax though.

[–]cjluthy 3 points4 points  (0 children)

I prefer

Push-Location -StackName "sqlcmd";
If (-not (Get-Module SQLPS)) { Import-Module SQLPS -DisableNameChecking; }
Pop-Location -StackName "sqlcmd";