SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Set_Login Error in powershell script


Set_Login Error in powershell script

Author
Message
rollercoaster43
rollercoaster43
SSC-Enthusiastic
SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)

Group: General Forum Members
Points: 127 Visits: 435
Hi All,

I was trying to automate my Password reset activity for Sql Logins from a central servr using Powershell.

With the help of some useful tips from the below link, I was able to create a script : http://www.mssqltips.com/sqlservertip/1947/connect-to-sql-server-via-windows-powershell-with-sql-server-authentication/

Initially I am just trying to read from the csv file and print the details to check if it works properly. Here is the script :

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
#imports the csv file with details

$Items = Import-CSV D:\TEMP\test.csv -Delimiter "|" #Use the chosen delimiter
ForEach ($Item in $Items)
{

$Instance = $Item.ServerName
$uname = $Item.uname
$pwd = $Item.pwd
$newpwd = $Item.newpwd

$srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Instance

#This sets the sql authentication

$srv.ConnectionContext.LoginSecure=$false;


#This sets the login name

$srv.ConnectionContext.set_Login($uname);


#This sets the password

$srv.ConnectionContext.set_Password($pwd)

echo $Instance
echo $uname
echo $pwd
echo $newpwd





}


This script works fine and prints everything as desired. However, there is an error that pops up in the powershell window and I would like to handle this error before I proceed.

The error is as below :

Exception calling "set_Login" with "1" argument(s): "Cannot apply value `null' to property Login: Value cannot be null
."
At line:11 char:33
+ $srv.ConnectionContext.set_Login <<<< ($uname);
+ CategoryInfo : NotSpecified: (Smile [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException



Can someone please let me know if there is anything wrong that I am doing here?

Thanks a Lot !!!
Gary Varga
Gary Varga
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16483 Visits: 6534
The following checks that you have values for each of the four variables:


if(($Instance) -and ($uname) -and ($pwd) -and ($newpwd))
{
$srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Instance

#This sets the sql authentication
$srv.ConnectionContext.LoginSecure=$false;

#This sets the login name
$srv.ConnectionContext.set_Login($uname);

#This sets the password
$srv.ConnectionContext.set_Password($pwd)
}
else
{
# Handle missing value(s) condition.
}


Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
rollercoaster43
rollercoaster43
SSC-Enthusiastic
SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)

Group: General Forum Members
Points: 127 Visits: 435
Thanks..

I will check with this condition and get back to let you know how it works now Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search