Powershell Won't Exit After Running Cmd.exe - vba

I'm using powershell to
Close Access Database,
Download updated Access front end to local machines,
Update (overwrite) local documents, and
Launch the Access database with parameters using cmd.exe.
Everything works fine, but the Exit command doesn't work after using cmd.exe command to launch the database.
If I comment out the cmd.exe command, then the Exit command works just fine. But if I use the Exit command, the script stops there and the Exit command does not work. Below is the entire code that I'm talking about.
## Close Microsoft Access
Stop-process -name MSACCESS -Force
## Download updated Access database to local machine
Copy-Item "F:\New_DB\Win7DBDocs\WC_Sys.mdb" -Destination "C:\DB_Docs" -Recurse -Force
## Copy Documents and Spreadsheets to local machine
echo "Overwriting files C:\DB_WPDocs"
Copy-Item -Path "F:\New_DB\DB_WPDocs\*" -Destination "C:\DB_WPDocs" -Recurse -Force
## Launch Microsoft Access with Parameters
cmd.exe /c "C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" "C:\DB_Docs\WC_Sys.mdb" /WRKGRP "F:\DB_Docs\Secured.mdw"
Exit

cmd.exe will block until MSACCESS.EXE exits.
To make cmd.exe launch the program and return immediately, use the start command:
cmd.exe /c start "C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" "C:\DB_Docs\WC_Sys.mdb" /WRKGRP "F:\DB_Docs\Secured.mdw"
... or drop cmd.exe completely and use the Start-Process cmdlet instead:
Start-Process "C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" -ArgumentList "C:\DB_Docs\WC_Sys.mdb", /WRKGRP, "F:\DB_Docs\Secured.mdw"

Related

Sequential Running of Scripts in SQL Server (without using a batch file)

I have 30 scripts of SQL code that I have been told to execute one by one (in a certain order) within SQL Server.
I am unable to use a batch file to do so (for reasons out of my control) and from another Question/Answer on this site I believe I can do something like the following:
In the SQL Management Studio open a new query and type all files as below
:r c:\Scripts\script1.sql
:r c:\Scripts\script2.sql
:r c:\Scripts\script3.sql
Go to Query menu on SQL Management Studio and make sure SQLCMD Mode is enabled
Click on SQLCMD Mode; files will be selected in grey as below
:r c:\Scripts\script1.sql
:r c:\Scripts\script2.sql
:r c:\Scripts\script3.sql
Now execute
My problem is my files have whitespace in their names for example:
'7. Address and Phone Number.sql'
I have been toying around with it trying to get the above method to work to no avail, any help would be greatly appreciated.
Consider most any programming language (Java, C#, C++, Python, PHP, VB) to iterate through scripts in file directory and call sqlcmd at command line. Recall sqlcmd is a separate executable tool with MSSQL installations, allowing command line interfacing to SQL Server (apart from IDEs like SSMS) and is available in both Windows and Linux installations.
Specifically, follow how to Run Transact-SQL Script Files. For batch jobs, use command line interpreters like Window's PowerShell and Unix's Bash.
PowerShell (save as .ps1 and run in PS ISE or PS terminal)
Get-ChildItem "C:\Scripts" -Filter *.sql |
Foreach-Object {
Write-Host $_.FullName
Start-Process -FilePath "sqlcmd" -S myServer\instanceName -i $_.FullName
# Invoke-Expression "& sqlcmd -S myServer\instanceName -i $_.FullName"
}
Bash (save as .sh and run in IDE or terminal)
#!/bin/bash
for file in C:/Scripts/*.sql; do
[ -e "$file" ] || continue
./sqlcmd -S myServer\instanceName -i "$file"
done

SQL BCP within Powershell hangs

I am new to BCP. I am tying to launch bcp.exe utility from Powershell but it hangs. Same command works fine command prompt. I am using Invoke-Expression to launch bcp.exe.
I am able to launch SQLCMD.exe without any problems.
This is my powershell.
Set-Location -Path "C:\Program Files\Microsoft SQL Server\110\Tools\Binn"
SQLCMD.EXE -b -E -S CORPSYSSQLDEV -d CORPSYSDM -Q "select top 10 * from t_test"
$psCommand = "bcp.exe ""testDB.dbo.t_test"" in ""C:\temp\test\testFile20180919.txt"" -c -t""\t"" -T -S ""TESTSQLDEV"" -e c:\temp\NoahFolder\error.csv"
Write-Host $psCommand
Invoke-Expression $psCommand
This is the result of $psCommand.
bcp.exe "testDB.dbo.t_test" in "C:\temp\test\testFile20180919.txt" -c -t"\t" -T -S "TESTSQLDEV" -e c:\temp\test\error.csv
Which works fine from command prompt but when I run the powershell script its stuck.
I am able to launch SQLCMD.exe from powershell fine.
What am I doing wrong? Any ideas or pointers.
I should have searched more before asking here. This post helped me. I was able to run BCP from powershell using Start-Process command.
$bcp = 'c:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe'
$arglist = #(
#add all arguments.
)
Start-Process -FilePath $bcp -ArgumentList $arglist

Getting error in SQL Server executing get-wmiobject

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

Powershell not exiting

cd "C:\apache-tomcat-7.0.67\bin"
.\shutdown.bat
.\startup.bat
I have above command in powershell file, but problem is that, powershell has not exited after .\startup.bat.
So, for solving this issue i have used following command, but then apache is not working for me
START "C:\apache-tomcat-7.0.67\bin\startup.bat"
If startup.bat does not exit, start it with Start-Process without the -wait parameter.
Start-Process .\startup.bat
(use the relative path in the working directory, not a global path)

Uninstalling IE 10 Using a batch file

I found a way to uninstall IE 10 using a batch file which below and it works great like it's suppose to. It uninstalls and restarts the computer after about a minute.
FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Windows-InternetExplorer-10..mum /c "cmd /c echo Uninstalling package #fname && start /w pkgmgr /up:#fname /norestart /quiet"
shutdown -r
But I wanted to take this further and Hide IE 10 from windows updates which will prevent windows to install this update actually in the future and to do that, Microsoft provides an executable file which you can download from here
http://www.microsoft.com/en-us/download/details.aspx?id=36512 After extracting this executable, you get 3 files, IE10_Blocker.adm, IE_blocker.cmd and IE10_BlockerHelp.htm and Microsoft instructions are "In the Command Prompt, goto the folder where you have extracted these 3 files and type “ie9_blocker.cmd /B” and hit Enter to set the blocker on the machine."
I decided to make one batch file for all this so right now I got this far.
FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Windows-InternetExplorer-10..mum /c "cmd /c echo Uninstalling package #fname && start /w pkgmgr /up:#fname /norestart /quiet"
mkdir “C:\IE10”
copy /Y \file01p\Users\test\public\IE Update blocker\IE10*.* “C:\IE10”
start /d C:IE10\IE10_Blocker.cmd /b
shutdown -r
mkdir “C:\IE10” This command is suppose to create the directory called IE10 on C drive
copy /Y \file01p\Users\test\public\IE Update blocker\IE10*.* “C:\IE10” This command will copy those executable from the network public folders to their C:\IE10 which I just created in the previous step.
This script doesn't work. Only up to uninstalling IE 10 it works but it doesn't create the directory and so it doesn't copy the files from the network. How can do this?
Thanks
Thanks
Try running your batch as administrator.
in an administrator : command prompt it makes the directory without issue.
also, it could be that you do not have access to make the directory on C:
you could try locating it elsewhere that you are sure you have write access.