Get-ChildItem ".\Stored Procedures\*.sql" | ForEach-Object { sqlcmd -S ServerName -d DatabaseName -E -i $_.FullName }
When I run a batch of scripts from a folder with the above command, if a problem persists in the intermediate script (like create/Alter/DROP DML script in between) then it should stop there only and need to give me an error message.
You'll need to do a few things:
Set ErrorActionPreference to stop
Use the -b parameter with sqlcmd.exe utility
Capture and log or display output of sqlcmd.exe utility
I answered a similar a question on another forum and I've re-posted the answer here:
echo "select 'Good 1'" > C:\temp\scripts\1.sql
echo "select * from missingTable" > C:\temp\scripts\2.sql
echo "Select 'Good 3'" > C:\temp\scripts\3.sql
$ErrorActionPreference = "Stop"
ForEach ($S In Gci -Path "C:\Temp\Scripts\" -Filter *.sql | Sort-Object Name) {
try {
$result = SqlCmd -b -S $env:computername\sql1 -i $S.FullName
$result = $result -join "`n"
if ($LASTEXITCODE -ne 0) {
throw "$S.FullName : $lastexitcode : $result"
}
else {
write-output "Success: $($s.fullname) : $result" | Out-File C:\Temp\Scripts\sqllogging.txt -Append
}
}
catch {
write-output "Failed: $_ " | Out-File C:\Temp\Scripts\sqllogging.txt -Append
throw
}
}
Related
Hi i'm new to this and want to capture exception and store values in SQL database but its not capturing.
My current flow is something like this:
My code:
try
{
try
{
$path='D:\'+$name # location not present in my system to throw exception
"$(get-date -format "yyyy-MM-dd HH:mm:ss"):Folder Name : $($name)" | out-file $LogFile -Append
if (Test-Path -Path $path)
{
$status='already exists'
"$(get-date -format "yyyy-MM-dd HH:mm:ss"):Folder Already Exists" | out-file $LogFile -Append
write-output $status
$msg='Already Existed'
}
#else
#{
#Creating a folder
#New-Item -Path $path -ItemType Directory
#"$(get-date -format "yyyy-MM-dd HH:mm:ss"):Folder Created" | out-file $LogFile -Append
#$status='Success'
#$msg='Successfully Created'
#}
}
catch
{
#to catch above exceptions
$status='Failed'
Write-Host "`nError Message: " $_.Exception.Message
Write-Host "`nError in Line: " $_.InvocationInfo.Line
Write-Host "`nError in Line Number: "$_.InvocationInfo.ScriptLineNumber
Write-Host "`nError Item Name: "$_.Exception.ItemName
"$(get-date -format "yyyy-MM-dd HH:mm:ss"):$($_.Exception.Message)" | out-file $LogFile -Append
$msg=$_.Exception.Message
}
$CheckQuery="SELECT COUNT(1) AS TABLECOUNT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = '$($name)'"
$InsertQuery="INSERT INTO [$($Database)].[dbo].[$($name)]
([DATE],[HOST_NAME],[STATUS],[MESSAGE])
VALUES('$date','$ComputerName','$status','$msg')
"
$CreateQuery='CREATE TABLE '+$($name)+' (DATE NVARCHAR(MAX),HOST_NAME NVARCHAR(MAX),STATUS NVARCHAR(MAX),MESSAGE NVARCHAR(MAX))'
#other sql part making connection and calling queries
}
catch
{
#catching SQL part exceptions
Write-Host "`nError Message: " $_.Exception.Message
Write-Host "`nError in Line: " $_.InvocationInfo.Line
Write-Host "`nError in Line Number: "$_.InvocationInfo.ScriptLineNumber
Write-Host "`nError Item Name: "$_.Exception.ItemName
"$(get-date -format "yyyy-MM-dd HH:mm:ss"):$($_.Exception.Message)" | out-file $LogFile -Append
}
Exception I'm getting in powershell is
But in SQL it stores blank data as shown in below image
As I'm new to this ,any help will be thankfull.
I'm trying to pass a variable from my Powershell Script to SQLPlus.
I've defined my variable as $csvStorage (the file path to the folder "csv_files"):
Powershell Script:
# Set file path to C:\xxxx\xxxx\xxxx\xxxx\csv_files
$filePath = Split-Path -Path $directory -Parent
$csvStorage = Join-Path $filePath -ChildPath "csv_files"
I have then passed it as an argument to the SQL script:
Powershell Script:
# Run sql_queries.sql whilst passing C:\xxxx\xxxx\xxxx\xxxx\csv_files\ into the SQL script
$queryTables = "/c sqlplus $dbUsername/$dbPassword#$dbServiceName #$filePath $csvStorage"
&$sqlPlus $queryTables
Then finally, referenced the variable in my SQL using '&1':
SQL Script:
set null null
set heading on
set pagesize 50000
set termout on
spool &1\hc_actual_vs_shadow_inv.csv
SELECT *
FROM hc_actual_vs_shadow_inv
/
spool off
/
exit
However, the query is not being executed and no .csv file is outputted. But I can't see what I'm doing wrong. Any assistance would be much appreciated.
Thanks
<#
.SYNOPSIS
This script executes all sql files from the specified directory and creates separate csv files in a specified directory.
Author: Dmitry Demin dmitrydemin1973#gmail.com
.DESCRIPTION
In the script, the format for displaying the date and decimal separator is configured.
.PARAMETER username
Specify the username for example SCOTT
.PARAMETER password
Specify the password for example TIGER
.PARAMETER connect_string
Specify the connect_string(TNS alias) for connect to database from $ORACLE_HOME/network/admin/tnsnames.ora.
.PARAMETER sql_path
Specify the directory for executing sql scripts.
.PARAMETER csv_path
Specify the directory for output csv.
.PARAMETER log_path
Specify the log file.
.EXAMPLE
This script executes all sql files from the specified directory and creates separate csv files in a specified directory.
.\run_export_all_tables.ps1 -username SCOTT -password tiger -connect_string ORCL -sql_path C:\export\sql\ -csv_path C:\export\csv\
#>
param(
[string]$username = "scott",
[string]$password = "tiger",
[string]$connect_string = "192.168.0.166:1521/TEST",
[string]$sql_path="C:\upwork\powershell_sqlplus_export_csv\sql\",
[string]$csv_path="C:\upwork\powershell_sqlplus_export_csv\csv\",
[string]$log_path="C:\upwork\powershell_sqlplus_export_csv\log_file.log"
)
# Column separator for csv file
$COLSEP=";"
# NLS_NUMERIC_CHARACTERS
$NLS_NUMERIC_CHARACTERS=".,"
$NLS_DATE_FORMAT="DD.MM.YYYY HH24:MI:SS"
#[string]$connect_string = "server2003ora10:1521/ORCL"
# Log file
$full_sql_path=$sql_path
$full_csv_path=$csv_path
$full_log_path=$log_path
#csv file extension
$csv_ext=".csv"
#Set NLS_LANG for session sqlplus
#"RUSSIAN_CIS.UTF8"
#"RUSSIAN_CIS.CL8MSWIN1251"
#"AMERICAN_AMERICA.UTF8"
#$NLS_LANG="RUSSIAN_CIS.CL8MSWIN1251"
$NLS_LANG="AMERICAN_AMERICA.CL8MSWIN1251"
#$NLS_LANG="AMERICAN_AMERICA.UTF8"
#Set NLS_LANG for session sqlplus
[Environment]::SetEnvironmentVariable("NLS_LANG",$NLS_LANG , [System.EnvironmentVariableTarget]::PROCESS)
$env_path_NLS=[Environment]::GetEnvironmentVariable("NLS_LANG", [EnvironmentVariableTarget]::PROCESS)
echo "SET session NLS_LANG: $env_path_NLS" | tee-object -Append -filepath $full_log_path
$SqlQueryExportTable1 =
#"
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set numwidth 30
set linesize 10000
set pagesize 0
SET COLSEP '$COLSEP'
ALTER SESSION SET NLS_NUMERIC_CHARACTERS='$NLS_NUMERIC_CHARACTERS';
ALTER SESSION SET NLS_DATE_FORMAT='$NLS_DATE_FORMAT';
"#
$SqlQueryExportTable2 =
#"
exit
"#
function Check_File
{
param (
[string]$pathfile
)
try {
$A=Get-Content -Path $pathfile -ErrorAction Stop
}
catch [System.UnauthorizedAccessException]
{
#Write-Host "File $pathfile is not accessible."
echo "File $pathfile is not accessible." | tee-object -Append -filepath $full_log_path
exit
}
catch [System.Management.Automation.ItemNotFoundException]
{
#Write-Host "File $pathfile is not found."
echo "File $pathfile is not found." | tee-object -Append -filepath $full_log_path
exit
}
catch {
Write-Host "File $pathfile. Other type of error was found:"
#Write-Host "Exception type is $($_.Exception.GetType().Name)"
echo "Exception type is $($_.Exception.GetType().Name)" | tee-object -Append -filepath $full_log_path
exit
}
}
echo "===========================================================================================" | tee-object -Append -filepath $full_log_path
$date_time_start = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$date_time_log = Get-Date -Format "yyyyMMddHHmmss"
Write-host "Script start time : $date_time_start "
try
{
echo "Script start time : $date_time_start ">>$full_log_path
}
catch {
Write-Host "Log File $full_log_path. Other type of error was found:"
Write-Host "Exception type is $($_.Exception.GetType().Name)"
exit
}
#chcp 1251
$files_input = Get-Childitem -File $full_sql_path
foreach ($file_input in $files_input)
{
echo "Found SQL file $file_input " | tee-object -Append -filepath $full_log_path
$full_sql_path_file=$full_sql_path+$file_input
$user_tab= get-content -Path $full_sql_path_file | out-string
echo "Found SQL : $user_tab " | tee-object -Append -filepath $full_log_path
$sqlQuery_show_table_all=""
$sqlQuery_show_table_all=$SqlQueryExportTable1+ $user_tab+ $SqlQueryExportTable2
$full_csv_path_file=$full_csv_path + $file_input + "_" + $date_time_log + $csv_ext
echo "-------------------------------------------------------------------------------------------" | tee-object -Append -filepath $full_log_path
echo "For SQL file : $full_sql_path_file will be created new csv file: $full_csv_path_file" | tee-object -Append -filepath $full_log_path
echo "Script will run for SQL: $user_tab " | tee-object -Append -filepath $full_log_path
$sqlOutput_tab = $sqlQuery_show_table_all | sqlplus -s $username/$password#$connect_string
$sqlOutput_count = $sqlOutput_tab.count
if ($sqlOutput_tab.count -gt 0)
{
Out-File -filepath $full_csv_path_file -append -inputobject $sqlOutput_tab -encoding default
echo "Exported rows: $sqlOutput_count " | tee-object -Append -filepath $full_log_path
}
else
{
echo "No exported rows: 0 row" | tee-object -Append -filepath $full_log_path
echo "$full_csv_path_file file not created " | tee-object -Append -filepath $full_log_path
}
echo "-------------------------------------------------------------------------------------------" | tee-object -Append -filepath $full_log_path
}
I am trying to use Invoke-AzVMRunCommand as a job. when I executed below script the job is created and executed successfully but I am failing to write the output like which job result belongs to which vm.
Invoke-AzVMRunCommand is used to invoke a command on a particular VM. You should have this information beforehand.
Here is some information on -AsJob parameter
https://learn.microsoft.com/en-us/powershell/module/az.compute/invoke-azvmruncommand?view=azps-2.6.0#parameters
As suggested by AmanGarg-MSFT, you should have that information before hand. You can use a hashtable $Jobs to store the server name and Invoke-AzVMRunCommand output and later iterate through using the $Jobs.GetEnumerator().
$Jobs = #{}
$Servers = "Server01","Server02"
[System.String]$ScriptBlock = {Get-Process}
$FileName = "RunScript.ps1"
Out-File -FilePath $FileName -InputObject $ScriptBlock -NoNewline
$Servers | ForEach-Object {
$vm = Get-AzVM -Name $_
$Jobs.Add($_,(Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -Name $_ -CommandId 'RunPowerShellScript' -ScriptPath $FileName -AsJob))
}
I want to read a file that contains a line separated list of *.sql file names (all located at the same directory) to execute the following:
$reader = [System.IO.File]::OpenText("_Scripts.txt")
try {
for(;;) {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
#output
$out = $line.split(".")[0] + ".txt" ;
# -serverinstance u should change the value of it according to use
invoke-sqlcmd -inputfile $line -serverinstance "." | format-table | out-file -filePath $out
$line
}
}
finally {
$reader.Close()
}
I'm trying to execute this script file by using a batch file containing the command:
powershell.exe -ExecutionPolicy Bypass -Command "_scripts.ps1"
but I get the error shown below:
Can anyone help me fix my ps1 script please?
This works for me:
$lines = Get-Content C:\Temp\TEST\_Scripts.txt
ForEach ($line in $lines)
{
$out = $line.split(".")[0] + ".txt" ;
Invoke-Sqlcmd -InputFile $line -ServerInstance "localhost" -Database "master" | Format-Table | Out-File -FilePath $out
}
I am using this website (http://npr.me.uk/scripting.html) to connect to telnet and run command. It returns me some information. I need to get this info every 4 seconds. How do I do that? Now it runs but reconnects everytime, so I have to wait while it opens a connection and it takes much more than 4s. Bat file:
echo off
cls
if exist r1.txt del r1.txt
if exist r2.txt del r2.txt
tst10.exe /r:stats.txt /o:r1.txt /m
for /f "skip=30 tokens=*" %%A in (r1.txt) do echo %%A >> r2.txt
del r1.txt
start r2.txt
And stats file:
192.168.xxx.xxx
WAIT "login:"
SEND "myuser\m"
WAIT "Password:"
SEND "mypass\m"
WAIT ">"
SEND "mycommand\m"
WAIT ">"
Use Powershell to program using a csv file with the connections, I am using it for re-programming mfd's
I have a file mfd.txt and a script that reads it in.
I have a telnet script template to change the settings on the mfd and the powershell script creates custom scripts for each mfd and sets dns and hostname parameters. When run, a logfile is piped into a directory for checking later
Script is as follows:
#Process for updating devices quickly using telnet
#Check file exists
c:
cd 'C:\Resources\Telnet'
cls
$fileisthere = $false
$fileisthere = test-path 'C:\Resources\Telnet\mfds.csv'
if ($fileisthere -ne $true)
{
""
Write-Host ("There is no MFD import list C:\Resources\telnet\mfds.csv") | out-file -filepath $logfile -force
""
exit
}
Write-Host ("MFD import List is present")
# for each device in devices:
$mfds = import-csv 'C:\Resources\Telnet\mfds.csv'
foreach ($mfd in $mfds)
{
# ping device and check for response
$mfdname = $mfd.name
$mfdip = $mfd.ipaddress
$mfddns1 = $mfd.dns1
$mfddns2 = $mfd.dns2
$mfdhostname = $mfd.serial
""
Write-Host ("Updating device $($mfdname) on IP address $($Mfdip) ")
""
("Updating device $($mfdname) on IP address $($Mfdip) ") | out-file -filepath $logfile -Append -force
if(!(Test-Connection -Cn $mfdip -BufferSize 16 -Count 1 -ea 0 -quiet))
{
Write-Host ""
Write-Host ("MFD $($mfdname) is offline or not at this address")
Write-Host ""
"" | out-file $logfile -Append -force
("MFD $($mfdname) is offline or not at this address") | out-file $logfile -Append -force
"" | out-file $logfile -Append -force
}
else
{
#find replace script
# Device is present and add to script header
$tststring = "$($mfdip) 23"
$tstfile = "$($mfdname)-$($mfdip).txt"
$tstlogfile = "$($mfdname)-$($mfdip).log"
$tststring | out-file $tstfile -force
type dns.txt >> $tstfile
$location1 = "C:\Resources\telnet\$($tstfile)"
$change1 = get-content $location1
$change1 | ForEach-Object { $_ -replace "dns 1 server", "dns 1 server $($mfddns1)"} | Set-Content $location
$location2 = "C:\Resources\telnet\$($tstfile)"
$change2 = get-content $location2
$change2 | ForEach-Object { $_ -replace "dns 2 server", "dns 2 server $($mfddns2)"} | Set-Content $location
$location3 = "C:\Resources\telnet\$($tstfile)"
$change3 = get-content $location3
$change3 | ForEach-Object { $_ -replace "hostname ether name", "hostname ether name $($mfdhostname)"} | Set-Content $location
$location4 = "C:\Resources\telnet\$($tstfile)"
$change4 = get-content $location4
$change4 | ForEach-Object { $_ -replace "devicename name", "devicename name $($mfdhostname)"} | Set-Content $location
# Create variables for update
Write-Host ("Updating $($Mfdname) on IP Address $($mfdIP) ")
$parameter1 = "/r:$($tstfile)"
$parameter2 = "/o:$($tstlogfile)"
#& cmd tst10 $parameter1 $paremeter2
write-host ("$($tstfile) $($tstlogfile)")
new-item $tstfolder -Type directory
move-item $tstfile $tstfolder
move-item $tstlogfile $tstfolder -ErrorAction SilentlyContinue
}
}