Jaspreet Saini
Jaspreet Saini

Reputation: 61

Powershell script as a step in sql job giving error

I am trying to create a sql job which syncs users from a csv file to ad group. My powershell script is one of the steps of this job. Issue is that my script is supposed to run on another server which has Active Directory but i keep on getting error when i run this step.

My script is following:

invoke-Command -Session Server-Name
Import-Module activedirectory
$ADUsers = Import-csv \\Server-Name\folder\file.csv
foreach ($User in $ADUsers)
{  
    $Username = $User.sAMAccountName
   $group=$user.adgroup 

if (Get-ADUser -F {SamAccountName -eq $Username})
{
 foreach($group in $groups){Add-ADGroupMember -identity $group -Members $Username}

    Write-Output "$username has beeen added to group $group"

  }
}

Error i am getting is

Executed as user: Username. A job step received an error at line 2 in a PowerShell script. The corresponding line is 'Invoke-Command -Session Server-Name. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot bind parameter 'Session'. Cannot convert the "Server-Name" value of type "System.String" to type "System.Management.Automation.Runspaces.PSSession". '. Process Exit Code -1. The step failed.

server name has '-' in between so need to know if that is causing the issue or i am using wrong way to run this script on a different server from a sql job

Any help would be appreciated!

Upvotes: 0

Views: 1050

Answers (1)

ErGaurav
ErGaurav

Reputation: 81

Jaspreet I am not expert on powershell but seems like you are passing the wrong parameters.Just referring to Microsoft docs seems like you need to pass the computer name rather than -Session Try with this line of code at starting invoke-Command -ComputerName Server-Name. For more please refer Microsoft docs https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6#examples

Upvotes: 2

Related Questions