I have a few .ps1 files being called from sql jobs that used to work and now i'm getting an error when the SQLPS module tries to load. The error specifically says: "...job step recieved an error at line 1 in Powershell script. The corresponding line is 'import module SQLPS -DisableNameChecking."
The job step is running like this -
Start-Process PowerShell -ArgumentList "& 'C:\NewInstanceScripts\SPLA_Users.ps1'"
The contents of the .ps1 file are as follows. This and others like it work fine in ps direct. just not when called from a sql job.
#Pull all group members into csv file
Get-ADGroupMember “CRDS Users” | where {$_.objectclass-eq “user”} | Select SamAccountName | Export-csv -path C:\SPLA_Users.csv -NoTypeInformation
#Insert contents of csv into SQL table
$sqlsvr = 'SQL03'
$database = 'db_SPLA'
$table = 'tbl_CRDSUsers'
#Create SQL Connection
Write-Verbose "Creating SQL Connection"
$conn = New-Object System.Data.SqlClient.SqlConnection("Server=$SQLSvr;Database=$Database;Integrated Security=True")
$conn.Open()
$cmd = $conn.CreateCommand()
#Create a delete statement if want to replace contents each time
#$cmd2 = $conn.CreateCommand()
#$cmd2.commandtext = "Delete From $table"
#Write-Host -Fore Green "Clean Table Contents"
#$cmd2.ExecuteNonQuery()
Import-Csv C:\SPLA_Users.csv | % {
#$_.Displayname
#$_.Version
#Create query string
#Must matching the table layout (SoftwareName, Version)
Write-Host -Fore Green "Inserting $_.SamAccountName into Table"
$cmd.CommandText = "INSERT INTO $table (SamAccountName,UploadDate) VALUES ('$($_.SamAccountName)', '$(get-date -f MM-dd-yyyy)')"
#Execute Query
$cmd.ExecuteNonQuery() | Out-Null
}
The only thing i changed was the version of SQL Server 2012