Getting error in SQL Server executing get-wmiobject - sql

I am trying to execute following PowerShell query in SQL Server
xp_cmdshell "get-WmiObject Win32_LogicalDisk -AV-RISCVMSQL114 | Format-Table -Property DeviceID,FreeSpace,Size"
After executing this, I am getting error:
'get-WmiObject' is not recognized as an internal or external command,
operable program or batch file.
I want to execute this command in all SQL Servers. It's getting executed on SQL Server 2008, but on SQL Server 2012, it's throwing that error due to version or some PowerShell component is not installed.
Can you to please look at the code and let me know

Try:
xp_cmdshell 'PowerShell.exe -noprofile -command "get-WmiObject Win32_LogicalDisk -AV-RISCVMSQL114 | Format-Table -Property DeviceID,FreeSpace,Size"'
If this doesn't work logon to the server open PowerShell with administrator rights. Type Get-ExecutionPolicy and check that Execution Policy is set to Unrestricted. If it isn't then enter the following, then type 'A' (Yes to All):
Set-ExecutionPolicy Unrestricted
If this still doesn't work try upgrading to at least PowerShell V3. But if this is not possible you can try repair your corrupt PowerShell V2 WMI repository with:
winmgmt /verifyrepository
If repository is found to be inconsistent run Winmgmt /salvagerepository and Winmgmt /resetrepository and verify after each. If it is still inconsistent try the following steps:
Go to Task Manager>> Services >> Open Service >> Windows Management Instrumentation service, and change the start up type to Manual.
Stop the WMI service:
net stop winmgmt
Rename the repository folder (C:\Windows\System32\wbem\Repository) to some else 'Repository_OLD'.
Start the WMI service:
net start winmgmt
Set the service back to auto start.
Failing Re-register all of the dlls and recompile the .mofs in the Wbem directory with the following .bat script and then reboot.
#echo off
sc config winmgmt start= disabled
net stop winmgmt /y
%systemdrive%
cd %windir%\system32\wbem
for /f %%s in (‘dir /b *.dll’) do regsvr32 /s %%s
wmiprvse /regserver
winmgmt /regserver
sc config winmgmt start= auto
net start winmgmt
for /f %%s in (‘dir /s /b *.mof *.mfl’) do mofcomp %%s

Related

SQL Server enters single user mode after cmd installation

I am installing SQL Server Express 2017 using the command line. Once the install is done I use a batch file to create a database. The database creation fails giving a message that : login with 'sa' failed, The server is in single user mode
I have used the installation syntax given on the msdn website and my DB creation script is a batch file which creates a DB after confirming that SQL 2017 is installed. I am logging the errors in a file which gives me the single mode error information. Below is the installation statement I am using through cmd
SETUP.exe /q /ACTION=Install /BROWSERSVCSTARTUPTYPE=Automatic /SQLSVCSTARTUPTYPE=Automatic /FEATURES=SQL /INSTANCENAME=SQLEXPRESS /SQLSVCACCOUNT="NT AUTHORITY\Network Service" /SQLSYSADMINACCOUNTS="BUILTIN\ADMINISTRATORS" /AGTSVCACCOUNT="NT AUTHORITY\Network Service" /SQLSVCINSTANTFILEINIT="True" /IACCEPTSQLSERVERLICENSETERMS /SAPWD=<Password> /SECURITYMODE=SQL
The code to install the DB is :
%OSQLEXE% -S %SERVER% -U %USERNAME% -P %PASSWORD% -i "%AgentPath%\CreateDatabase.sql" >> c:\InstallLog.txt
Ideally my DB should be created but for some reason I get this error. Can someone please help?

Running commands on Remote machine using PsExec from Msbuild invoked via Jenkins

I am using psexec(with MSbuild Script) to run an xCopy on a server from Jenkins(Hudson). The command runs ok when run through Command Prompt(as System user), but gives the following error when tried using Jenkins. This is the command I am using from Msbuild script:
psexec \\<RemoteMachine> /accepteula -i -u <Domian\User> -p <Pass> -s cmd /c xcopy <Path1> <Path2> /e /i
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ..
Couldn't access :
Connecting to ...
**error MSB3073: The command "psexec \\<RemoteMachine> /accepteula -i -u <Domian\User> -p <Pass> -s cmd /c xcopy <Path1> <Path2> /e /i " exited with code 6.**
I have tried too many options but no luck so far.
Does anyone tried the same using Jenkins? Help will be really appreciated.
Are there any other ways to achieve this using some other tools without any problem. Please share.
Just wanted to post the solution here, as I figured it after some time.
The Remote Machine should have this user added in the Administrators Group.
This will resolve the connection issue. :)

Installing Visual Studio Test Controller on Azure instance is not working

I already mentioned the problem in another thread, but I thought the root of the error was another. Now I know the real error, but cannot solve it, and don't understand it.
I want to install Visual Studio Test Controller on an Azure instance. I am trying to do that via a startup task, that runs a batch-file which in turn downloads the test controller installation file and installs it on the instance.
The problem is: that does not work when running the installation via startup task. When I log in to the machine via RDP and double click the exact same batch file everything works fine.
Here are some additional information:
This is my batch file I execute:
powershell $command = "set-executionpolicy Unrestricted"
powershell $command = ".\setupController.ps1" -NonInteractive >; out.txt
echo Setup TestController >> %~dp0out.txt
cd E:\approot
timeout /T 60
"E:\approot\testcontroller.exe" /full /q /Log %~dp0install.log
echo Setup has been executed! >> %~dp0out.txt
net user vstestagent MyPassword! /add
net localgroup Administrators vstestagent /add
REM Create a task that will run with full network privileges.
net start Schedule
schtasks /CREATE /TN "Configure Test Controller Service" /SC ONCE /SD 01/01/2020 /ST 00:00:00 /RL HIGHEST /RU vstestagent /RP MyPassword! /TR e:\approot\configcontroller.cmd /F
schtasks /RUN /TN "Configure Test Controller Service"
This is the startup task of the role:
<Task executionContext="elevated" taskType="background" commandLine="setupController.cmd"></Task>
The batch file, as well as the installation file of Test Controller are located in E:\approot folder
The installation logfile of testcontroller says something like that:
0A88:0ABC][2013-04-12T10:36:04]: Creating a system restore point.
[0A88:0ABC][2013-04-12T10:36:04]: System restore disabled, system restore point not created.
[0A88:0ABC][2013-04-12T10:36:04]: Caching bundle from: 'C:\Resources\temp\48c0ec5daf5846cab7f53f6b5c80dc6f.TestController\RoleTemp\{44f19fb6-98e0-4462-886c-c5e56d82dfd3}\.be\vstf_testcontroller.exe' to: 'D:\ProgramData\Package Cache\{44f19fb6-98e0-4462-886c-c5e56d82dfd3}\vstf_testcontroller.exe'
[0A88:0ABC][2013-04-12T10:36:04]: Registering bundle dependency provider: {44f19fb6-98e0-4462-886c-c5e56d82dfd3}, version:
11.0.51106.1
[0A40:09EC][2013-04-12T10:36:04]: MUX: Cache Begin
[0A40:064C][2013-04-12T10:36:05]: Error 0x80070070: Failed to set end of file.
[0A40:064C][2013-04-12T10:36:05]: Error 0x80070070: Failed to extract all files from container.
[0A40:09EC][2013-04-12T10:36:05]: Error 0x80070070: Faild to begin and wait for operation.
[0A40:09EC][2013-04-12T10:36:05]: Error 0x80070070: Failed to extract payload: a25 from container: WixAttachedContainer
[0A40:09EC][2013-04-12T10:36:05]: Failed to extract payloads from container: WixAttachedContainer to working path: E:\approot\testcontroller.exe, error: 0x80070070.
[0A40:0B98][2013-04-12T10:36:05]: Error 0x80070070: Failed while caching, aborting execution.
[0A88:0ABC][2013-04-12T10:36:05]: Removed bundle dependency provider: {44f19fb6-98e0-4462-886c-c5e56d82dfd3}
... Some Rollback and cleanup
[0A40:0B98][2013-04-12T10:36:06]: Variable: WixBundleTag = vstf_testcontroller,1031
[0A40:0B98][2013-04-12T10:36:06]: Variable: WixBundleVersion = 11.0.51106.1
[0A40:0B98][2013-04-12T10:36:06]: Exit code: 0x643, restarting: No
I really don't know where the problem is - especially because when I run the batch file via RDP everything works just fine.
The only difference seems to be the user esecuting the batch: Startup Tasks run with administrative priviledges and I think my RDP account is another, but also has administrative priviledges....
Best regards
Sebastian
I still don't know WHY it is that way, but the installation works, if you do it via a scheduled task.
Because I thought the only difference is maybe the account installing the software, I first tried to install the test controller via runas command, but I could not pass a password along with the command. So I created a scheduled task for installing the test controller and was able to pass user credentials along with the command. Now my setupController.cmd looks like this:
powershell $command = "set-executionpolicy Unrestricted"
powershell $command = ".\setupController.ps1" -NonInteractive >; out.txt
echo Setup TestController >> %~dp0out.txt
cd E:\approot
echo Setup has been executed! >> %~dp0out.txt
net user vstestagent MyPassword! /add
net localgroup Administrators vstestagent /add
REM Create a task that will run with full network privileges.
net start Schedule
schtasks /CREATE /TN "Install Test Controller Service" /SC ONCE /SD 01/01/2020 /ST 00:00:00 /RL HIGHEST /RU vstestagent /RP MyPassword! /TR "E:\approot\testcontroller.exe /full /q /Log %~dp0install.log" /F
schtasks /RUN /TN "Install Test Controller Service"
timeout /T 120
schtasks /CREATE /TN "Configure Test Controller Service" /SC ONCE /SD 01/01/2020 /ST 00:00:00 /RL HIGHEST /RU vstestagent /RP MyPassword! /TR e:\approot\configcontroller.cmd /F
schtasks /RUN /TN "Configure Test Controller Service"
Hope it helps somebody else also.

Passing CMD Results to Variable in a Batch File

I am trying to install an application and a group of services using PSTools, but I want to take into account that the computer I am trying to connect to may be turned off or on a different network, which is not reachable from the internal network.
Basically if the machine is not able to be accessed through the admin share, this is the message that I am getting:
Couldn't access MachineName:
The network path was not found.
Make sure that the default admin$ share is enabled on MachineName.
This is the syntax I am using to try to capture the "Error Message" and then report back that if installation was successful or not (depending on if it can contact the machine)
#echo off
set /p name=What is the machine name?:
psexec.exe \\%name% -u *useraccount* -p *password* \\ServerName\installation.bat
FOR /F "tokens=*" %%A IN ('COMMAND ^| FIND "Couldn't access"') DO SET Error=%%A
If "%Error%"=="Couldn't access"
ECHO Installation Failed.
Else
ECHO Installtion complete.
Pause
exit
Currently it hangs right at the point it's defining the Error Variable. Can't quite figure out what I am going wrong here.
'COMMAND ^| FIND "Couldn't access"' opens a command shell, which is why it hangs. It will not proceed until that shell is exited.
You will need to look at redirecting the error messages to another file. 2>Errors.txt on the psexec line will give you a file to search in the next line.
this will make the batch file look something like this:
#echo off
set /p name=What is the machine name?:
psexec.exe \\\%name% ... \\\ServerName\installation.bat 1>Error.txt 2>&1
for /f "tokens=*" %%A in ('FIND /i error.txt "Couldn't Access"') do SET Error=%%A
If not x%ERROR:Couldn=%==x%ERROR% (
ECHO Installation Failed.
) Else (
ECHO Installtion complete.
)
Pause
exit
(Also, notice the use of brackets to make a multi line IF)
the check for if will see if Couldn is part of the string, as a direct comparison will not work, as you would have to check against the whole string including the machine name

Cannot start a VB Windows Service from batch

I created a custom windows service in Visual Basic using Visual Studios '05.
The service is on our server which is running Windows Server 2003. I have installed the service, and it runs perfectly when I go into Computer Management and Start the service manually.
I have created a batch file to start the service using the NET START command. Whenever I run the batch file, the service doesn't start. I get this message from the command prompt:
The 'Service Name' is starting.
The 'Service Name' could not be started. The service did not report an error. More help is available by typing NET HELPMSG 3543.
I'm not sure what to do. (Been "googling" for a couple of hours.) It starts up when I manually run it, but it wont start via the batch file. Thanks!
Try forcing it to elevate in the batch file.
#ECHO OFF
:: Automatically elevate...
#ECHO OFF
SETLOCAL
:: Check if script was run with administrator privilages
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
:: If the error flag is set, we do not have admin.
IF "%ERRORLEVEL%"=="0" GOTO GOTADMIN
:UACPROMPT
ECHO. Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
ECHO. UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%TEMP%\getadmin.vbs"
EXIT /B
:GOTADMIN
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
CD /D "%~dp0"
ENDLOCAL
GOTO SCRIPTSTART
:: THE ACTUAL SCRIPT STARTS HERE
:SCRIPTSTART
NET START [whatever service]