Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to Connect to One Server, Extract Data, Connect to Second Server, and Insert that Extracted Data into Table on Second Server

$
0
0
I have the following code that connect to my Central Management Server and loops through each server extracting data and inserting into a table on a different server.  I only want to connect to one server, extract data, connect to a second server, and insert that data into a table on that second server.

I am new to PowerShell and I have been trying to tweek the code below to do this.  However, I have been unsuccessful.  Can anyone help me learn how to do this or point me to a web page demonstrating this?




<#
.SYNOPSIS
Creates a DataTable for an object
.DESCRIPTION
Creates a DataTable based on an objects properties.
.INPUTS
Object
    Any object can be piped to Out-DataTable
.OUTPUTS
   System.Data.DataTable
.EXAMPLE
$dt = Get-Alias | Out-DataTable
This example creates a DataTable from the properties of Get-Alias and assigns output to $dt variable
.NOTES
Adapted from script by Marc van Orsouw see link
Version History
v1.0   - Chad Miller - Initial Release
v1.1   - Chad Miller - Fixed Issue with Properties
.LINK
http://thepowershellguy.com/blogs/posh/archive/2007/01/21/powershell-gui-scripblock-monitor-script.aspx
#>
function Out-DataTable
{
    [CmdletBinding()]
    param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)] [PSObject[]]$InputObject)

    Begin
    {
        $dt = new-object Data.datatable  
        $First = $true 
    }
    Process
    {
        foreach ($object in $InputObject)
        {
            $DR = $DT.NewRow()  
            foreach($property in $object.PsObject.get_properties())
            {  
                if ($first)
                {  
                    $Col =  new-object Data.DataColumn  
                    $Col.ColumnName = $property.Name.ToString()  
                    $DT.Columns.Add($Col)
                }  
                if ($property.IsArray)
                { $DR.Item($property.Name) =$property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1 }  
                else { $DR.Item($property.Name) = $property.value }  
            }  
            $DT.Rows.Add($DR)  
            $First = $false
        }
    } 
     
    End
    {
        Write-Output @(,($dt))
    }

} #Out-DataTable




Import-Module “sqlps” -DisableNameChecking



  

foreach ($RegisteredSQLs in dir -recurse SQLSERVER:\SQLRegistration\'Central Management Server Group'\CentralManagementServerNameHere\ | where {$_.Mode -ne "d"} ) 

  
$dt = Invoke-sqlcmd -ServerInstance "$($RegisteredSQLs.ServerName)" -Database "tempdb" -InputFile "D:\CMS\Scripts\T-SQL\DatabasesNotIncludedInAvailabilityGroup.sql" | out-DataTable
$dt
# Write data table to database using TVP 
$conn = new-Object System.Data.SqlClient.SqlConnection("Server=ServerWhereRepositoryDatabaseIs;DataBase=CMSRepository;Integrated Security=SSPI") 
$conn.Open() | out-null
"Connected"
$cmd = new-Object System.Data.SqlClient.SqlCommand("dbo.usp_InsertDatabasesNotIncludedInAvailabilityGroup", $conn) 
$cmd.CommandType = [System.Data.CommandType]'StoredProcedure'
#SQLParameter 
$spParam = new-Object System.Data.SqlClient.SqlParameter 
$spParam.ParameterName = "@TVP"
$spParam.Value = $dt
$spParam.SqlDbType = "Structured" #SqlDbType.Structured 
$spParam.TypeName = "DatabasesNotIncludedInAvailabilityGroup"
  
$cmd.Parameters.Add($spParam) | out-Null
$cmd.ExecuteNonQuery() | out-Null
$conn.Close() | out-Null


lcerni


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>