Powershell SQL administrator

Can the below code converted to Powershell ..any suggestions ,thanks.

-----------To get Users DB Role ----------------------------------------------------------------------------------------                
        
            Declare DBUser_Cur cursor for                
                Select     name from master.dbo.sysdatabases
                where Name not in ('tempdb','Pubs','Northwind','Model')
                For read only

            Open DBUser_Cur
            Fetch next from DBUser_Cur into @DBName
            While @@fetch_status = 0
            Begin
            --To get DB info
 
                If (Select isnull(object_id('tempdb..#DBUser_08'), 0)) = 0
                    Create table #DBUser_08 (
                    UserName varchar(150) null,
                    RoleName Varchar(1000) null,
                    LoginName Varchar(200) null,
                    DefDB Varchar(1000) null,
                    DefSchemaName varchar(500) null,
                    UserID int null,
                    SUserID int null
                        )
                Truncate table #DBUser_08
                        
                Declare @DBQuery varchar(200)
                
                Set @DBQuery = @DBName+'.dbo.sp_helpuser'

                  Insert into #DBUser_08 exec (@DBQuery)

                  Declare GRPName_CUR cursor for
                    Select UserName, RoleName, LoginName from #DBUser_08
                    where (UserName is not null and UserName not like '%##%' and UserName not in ('guest','sys','INFORMATION_SCHEMA') and UserName not like '%$%')
                    For read only
                
                    Open GRPName_CUR
                    
                    Fetch Next from GRPName_CUR into @UserName,@RoleName, @LoginName
                
                    While @@fetch_status = 0
                    Begin
                        
                        If @LoginName is null  -- To get Orphaned Users Info
                         Begin
                             select @DateCreated = ''
                             Set @UserStatus = 'InActive'
                             Select DBRole from LoginReport where UserName =@UserName and DBName =@DBName
                             If (@@ROWCOUNT =0)
                                Begin
                                    Insert into IBM_DBHEALTH.dbo.LoginReport
                                          (ServerName,[SQLInstanceName],[DBType],DBName,UserName,[UserStatus],DBRole,DateCreated)
                                    values(@ServerName,@SQLInstanceName,@DBType,@DBName,@UserName,@UserStatus,@RoleName,@DateCreated)
                                End
                              Else
                                Begin
                                    Select @DBCurrentRole = DBRole from LoginReport
                                    where [UserName] = @UserName
                                        and DBName =@DBName
                                    Set @DBNewRole = @DBCurrentRole +'\'+@RoleName
                            
                                    Update IBM_DBHEALTH.dbo.LoginReport
                                        Set DBRole =@DBNewRole
                                        Where [UserName] = @UserName
                                        and DBName =@DBName
                                        and [SQLInstanceName] = @SQLInstanceName
                                        and DBRole =@DBCurrentRole
                                End
                            End
                          Else  -- To get the regular Login/User info
                            Begin
                                select @DateCreated = createdate from sys.syslogins where name =@LoginName
                                Set @UserStatus = 'Active'
                                select @UserStatus = 'InActive' from sys.syslogins where name =@LoginName and (status = 10 or denylogin =1)
                        
                                Select DBRole from LoginReport where LoginName =@LoginName and DBName =@DBName
                                If (@@ROWCOUNT =0)
                                    Begin
                                        Insert into IBM_DBHEALTH.dbo.LoginReport
                                                (ServerName,[SQLInstanceName],[DBType],DBName,LoginName,UserName,[UserStatus],DBRole,DateCreated)
                                        values(@ServerName,@SQLInstanceName,@DBType,@DBName,@LoginName,@UserName,@UserStatus,@RoleName,@DateCreated)
                                    End
                                Else
                                    Begin
                                        Select @DBCurrentRole = DBRole from LoginReport
                                        where [LoginName] = @LoginName
                                            and DBName =@DBName
                                        Set @DBNewRole = @DBCurrentRole +'\'+@RoleName
                                
                                        Update IBM_DBHEALTH.dbo.LoginReport
                                            Set DBRole =@DBNewRole
                                            Where [LoginName] = @LoginName
                                            and DBName =@DBName
                                            and [SQLInstanceName] = @SQLInstanceName
                                            and DBRole =@DBCurrentRole
                                    End
                                End
                          
                     Fetch next from GRPName_CUR into @UserName,@RoleName, @LoginName
                    
                    End
                    Close GRPName_CUR
                    Deallocate GRPName_CUR
                    
                     Truncate table #DBUser_08
                  Fetch Next From DBUser_Cur into @DBName
                End  
            Close DBUser_Cur
            Deallocate DBUs

September 9th, 2015 5:43am

Yes but why?  If it works as SQL then why convert it?

Suggestion - create a stored procedure and call it from Power

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:50am

Thanks will see , i was trying to

Declare @DBName varchar(200)

Set @DBName=Select  name from master.dbo.sysdatabases where Name not in ('tempdb','Pubs','Northwind','Model')
  ---here iam geting error Sub query cannot --have multiple values
               
                      --To get DB info
 
                If (Select isnull(object_id('tempdb..#DBUser_08'), 0)) = 0
                    Create table #DBUser_08 (
                    UserName varchar(150) null,
                    RoleName Varchar(1000) null,
                    LoginName Varchar(200) null,
                    DefDB Varchar(1000) null,
                    DefSchemaName varchar(500) null,
                    UserID int null,
                    SUserID int null
                        )
                Truncate table #DBUser_08
                        
                Declare @DBQuery varchar(200)
                
                Set @DBQuery = @DBName+'.dbo.sp_helpuser'

                  Insert into #DBUser_08 exec (@DBQuery)

September 9th, 2015 6:10am

What were you trying?  I don't understand.
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 6:16am

I was trying to remove cursor and wanted to write in a query,May be iam doing something wrong here..

Thanks for your reply..

September 9th, 2015 6:20am

Are you asking for help with the SQL?  You cannot change SQL into PowerShell.  You can call a SQL script from PowerShell. You can convert the procedural SQL into a simple SELECT and then post process the results in PowerShell.

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 6:24am

In PowerShell we would just run the sp_helpuser as it will return a table object

$cmd.CommandText='exec sp_helpuser'
$adapt=New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$dt=New-Object System.Data.DataTable 
$adapt.Fill($dt)

$dt now has all of the data.

September 9th, 2015 6:29am

Thanks could you help me with SQL here , How to insert into #temp table ,from two diff query Like below..

Set @DBQuery = @DBName+'.dbo.sp_helpuser'
                  Insert into #DBUser_08 exec (@DBQuery)

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 6:52am

If you are asking for SQL help you are posting in the wrong forum.  Post in the TransactSQL forum.
September 9th, 2015 6:58am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics