Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Runs fine in the ISE but not when ran as a script?

$
0
0

$date = Get-Date -Format MM.d.yyyy
$OutputDir = “C:\Reports\SQL”
$File = “Sql_” + “$date” + “.csv”
$OutputFile = Join-Path $OutputDir $file

if(!(Test-Path -Path $OutputDir))
{mkdir $OutputDir}
else {continue}

#SQl Data—————————————————————————————————————–
#Connection Strings
$Database = “db1″
$Server = “server11″

# Connect to SQL and query data, extract data to SQL Adapter
$SqlQuery = “DECLARE @PreviousDay Date, @toDay Date
SET @PreviousDay = GETDATE() – 1
SET @toDay = GETDATE()
PRINT @PreviousDay
PRINT @toDay

SELECT Phone1

FROM db1.dbo.Lead (NOLOCK)
WHERE ClientrelationshipId IN (4963,118, 4964, 5109)
AND CreatedDate >= @PreviousDay
AND CreatedDate < @toDay
ORDER BY LeadId ”

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = “Data Source=$Server;Initial Catalog=$Database;Integrated Security = True”
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$nRecs = $SqlAdapter.Fill($DataSet)
$nRecs | Out-Null
#Populate Hash Table
$objTable = $DataSet.Tables[0]
#Export Hash Table to CSV File
$objTable |Export-CSV $OutputFile -NoTypeInformation
#SQl Data—————————————————————————————————————–

#ftp server upload——————————————————————————————————–
$Dir= $OutputFile
$ftp = “ftp://mysite.com/dir”
$user = “user”
$pass = “password”
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
#list every sql server trace file
foreach($item in (dir $Dir)){
“Uploading $item…”
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
}

#ftp server upload——————————————————————————————————–


Viewing all articles
Browse latest Browse all 13067

Trending Articles