SqlService WMI query in PowerShell returns nothing - sql-server

I'm trying to run a simple Get-WmiObject call in PowerShell.
Get-WmiObject -computerName $srv -namespace root\Microsoft\SqlServer\ComputerManagement -class SqlService
When running under an account that has administrator rights to the remote server I get no response. The command does complete, but no data is shown. When running this command under an account that does not have rights to the server I get an "Access Denied" message.
When I run this on the remote server directly I get "Invalid namespace", but the server is definitely our SQL Server 2008.
Why doesn't this kind of script return the object as expected?

Try
Get-WmiObject -computerName $srv -namespace root\Microsoft\SqlServer\ComputerManagement10 -class SqlService
On several machines I have, the namespace is ComputerManagement10.
BTW, just in case, you need to find the namespace names:
Get-WmiObject -ComputerName $srv -NameSpace root\Microsoft\SQLServer -Class "__NAMESPACE" | Select Name
The above command will tell you the right namespace names.

Have you checked services with get-service -computername $srv -include ā€œ*sql*ā€?
I would also check whether the class is present in:
Get-WmiObject -Namespace root\Microsoft\SqlServer\ComputerManagement10 -List

Related

-Credential parameter not working with Invoke-ASCmd

I'm using SqlServer module for powershell( https://www.powershellgallery.com/packages/SqlServer/21.1.18256) and I'm trying to use Invoke-ASCmd with -Credential parameter. The problem is, -Credential parameter is ignored and the current user for whom the powershell is started is used. When I start powershell with the user I want to use for -credential, it works fine.
Official docs say(https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-ascmd?view=sqlserver-ps):
"
-Credential
If this parameter is specified, the user name and password passed will be used to connect to specified Analysis Server instance. If no credentials are specified default windows account of the user who is running the tool will be used."
But it does not work that way.
$pwd=ConvertTo-SecureString "something" -AsPlainText -Force
$mycred = New-Object System.Management.Automation.PSCredential("someusername",$pwd)
Invoke-ASCmd -Server "someServer.somewhere" -Credential $mycred -InputFile "C:\path\to\xmla\1.xmla"
Any ideas on how to fix this?
looks like you are using an old version of PowerShell. Try 5.1

Inno script to identify SQL Server instances installed on client system and skip installing instance if already installed

I am creating a package using inno script and want to install SQL Server SQLEXPR_x64_ENU on the client's machine. I want code which determines installed SQL Server instances (MSSQL$MYSQL and MSSQLSERVER) on the target system. If the instances are already installed, then the script should skip installing the instances again.
I am using following shell script to find instances and calling it by batch file but it is not working.
$server = $env:computername
$object = Get-WmiObject win32_service -ComputerName $server | where {($_.name -like "MSSQL$*" -or $_.name -like "MSSQLSERVER" -or $_.name -like "SQL Server (*") -and $_.name -notlike "*helper*" -and $_.name -notlike "*Launcher*"}
if ($object)
{
echo " One OR More Instances running..."
$instInfo= $object |select Name,StartMode,State, Status
}
else
{
Start-Process -FilePath "C:\sql\SQLEXPR_x64_ENU.exe"
echo " No SQL Instances running..."
}
Is there any Inno script which can find number of SQL Server instances installed?

Script to stop/restart local SQL services

Does anyone have a simple script (powershell or command line) that would enable to me to shutdown local SQL Services (SQL Server, Integration services, etc ..) when I don't need them and then perhaps another script to turn them all back on when I do need them?
Currently I am going to Services and then stopping/starting them manually.
We had a need to stop and disable and then enable and start on multiple remote servers, so this is how I handled it:
Stopping:
function stopdisable ($compnam, $svc)
{
(get-service -computername $compnam -name $svc).stop()
set-service -computername $compnam -name $svc -startuptype disabled
}
stopdisable "server1" "servicename1"
stopdisable "server2" "servicename2"
Starting:
function enablestart ($compnam, $svc)
{
set-service -computername $compnam -name $svc -startuptype automatic
(get-service -computername $compnam -name $svc).start()
}
enablestart "server1" "servicename1"
enablestart "server2" "servicename2"
Of course, this is dependent upon you having the appropriate permissions on the remote servers.

PowerShell Silent Install Windows Application

To give the background, Iā€™m trying to automate silent install of our Windows application (setup.exe) on a remote machine via PowerShell scripts and it is getting stuck due to a dependency, in our case SQLExpress Edition 2005.
Since our application depends on SQLExpress, during the installation process it tries to install the SQL Server 2005 Express Edition and that's when it gets stuck forever. I can see the process (SQLExpress**.exe) in Task Manager and nothing happens after that. However this works just fine when I logged in to the remote machine and do a manual installation (run setup.exe myself) as well as run through the PowerShell script locally (so that verify no issues with setup.exe or PowerShell script)
I tried different things but of no success. There are no logs or messages anywhere. All I know is, it is stuck while the setup launches the installation of SQLExpress.
Any help will be highly appreciated.
PowerShell command:
Start-Process -FilePath "C:[myapplicatonname]\setup.exe" -ArgumentList '-s -f2"c:\LogFiles\setup.log" -K"XXXXXXX-XXXX-XXXXX;XXXX-XXXX-XXXXXX" -gS' -Wait -PassThru
PS script to silent install application remotely:
$username = "[username]"
$password = "[pwd]"
$cred = new-object -typename System.Management.Automation.PSCredential ` -argumentlist $username, $password
$testCon = Test-Connection [machine name]
echo ($testCon + "==========================")
$dc1 = New-PSSession -ComputerName \\machinename -Credential $cred
Enter-PSSession -Session $dc1
$script = {
$p = Start-Process -FilePath "C:\[application name]\setup.exe" -ArgumentList '-s -f2"c:\LogFiles\setup.log" -K"1A34AQ9-SAHYTH-UMA68;JA34AQ9-YLMT-C7THH" -gS' -Wait -PassThru
}
invoke-command -computername \\machinename -Credential $cred -scriptblock $script
if($p.ExitCode -ne 0)
{
Write-Host " successfully installed"
}
else
{
Write-Host "installer exit code $($p.ExitCode)"
}
Exit-PSSession

Powershell launch cmd file as administrator

I want to execute a .cmd file on a remote server, through Powershell. This works well:
Invoke-Command -ComputerName <computerName> -credential $cred -ScriptBlock {&"C:\...\Script.cmd"}
But what is the syntax to run Script.cmd "as administrator" ?
Your script block should be:
-Scriptblock {Start-Process -Path "C:\...\Script.cmd" -Verb Runas}
Check out the Start-Process cmdlet and the verb runas, that will get you there. Optionally, include -wait with that cmdlet to wait for the cmd to finish before returning.

Resources