Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need a way to script out LinkedServers on a SQL 2005\2008 boxes but place the script in seperate...


Need a way to script out LinkedServers on a SQL 2005\2008 boxes but place the script in seperate files.

Author
Message
Michael Robey
Michael Robey
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 121
If I run this it scripts them out all into one file. Can this be adjusted to put them in to individual files or is there a better way to do this?

Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
[system.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.sqlserver.smo’) |out-null
CLS
CD SQLSERVER:\SQL\OM37345\DEFAULT\LinkedServers
GCI | %{$_.script()} | out-file "C:\LinkedServers.sql"



SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21073 Visits: 18259
Will this work for you?
http://sev17.com/2010/07/scripting-linked-servers/



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8235 Visits: 14368
This worked for me:

Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue
Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.Smo’) | Out-Null

foreach($linkedServer in (Get-ChildItem SQLSERVER:\SQL\MYSERVER\DEFAULT\LinkedServers)) {
$linkedServer.Script() | Out-File ("C:\LinkedServer." + $linkedServer.Name.Replace(".","_").Replace("\","$") + ".sql")
}



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Michael Robey
Michael Robey
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 121
That will work perfectly. Thanks for the help as it is muchly appreciated.



Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8235 Visits: 14368
Anytime, happy to help :-D

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Michael Robey
Michael Robey
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 121
I created this script back in 2012 and it works fine running it under Windows XP, but now when I try to run it under my new machine Windows 7 it wont run. Could you tell me why this would be?

<# SYNOPSIS
Generates create scripts for all LinkedServers.
DESCRIPTION
PowerShell script using SMO to script out all LinkedServers.
NOTES
Requires: PowerShell Version 2.0, SMO assembly
#>
#Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue
#Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue

# Configuration data
#[string] $server = "SERVERNAME";    # SQL Server Name
#[string] $serverinstance = "SERVERNAME\DEV";   # SQL Server Instance Database with the tables to script out.
#[string] $database = "DBNAME";          # Database with the tables to script out.
#[string] $folder = "S:\DBA";       # Path to export to


param ($server, $serverinstance, $database, $folder)

# Reference to SMO
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') | Out-Null

Write-Output ((Get-Date -format yyyy-MM-dd_HH-mm-ss) + ": *** LinkedServers Started ***");

foreach($linkedServer in (Get-ChildItem SQLSERVER:\SQL\$Serverinstance\LinkedServers))
{
   $linkedServer.Script() | Out-File ($folder + "\" + $server + "\_ServerObjects\" + $linkedServer.Name.Replace(".","_").Replace("\","$") + ".sql")
}

Write-Output ((Get-Date -format yyyy-MM-dd_HH-mm-ss) + ": !!! LinkedServers Finished !!!");
Write-Output " "



Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8235 Visits: 14368
It could be one of a lot of different reasons. What is the exact error message or behavior you are seeing running on Windows 7?

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8235 Visits: 14368
Works on Windows 8 with SQL 2012 or 2008 R2:

<# SYNOPSIS
Generates create scripts for all LinkedServers.
DESCRIPTION
PowerShell script using SQL Provider to script out all LinkedServers.
NOTES
Requires: PowerShell Version 2.0, SMO assembly
#>
param ($server, $serverinstance, $database, $folder)

# Import SQL 2012 module:
Import-Module sqlps

#if you're still using SQL 2008/R2, comment the above call to Import-Module and load the 2008 provider instead:
#Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue

# Configuration data
#[string] $server = "SERVERNAME"; # SQL Server Name
#[string] $serverinstance = "SERVERNAME\DEV"; # SQL Server Instance Database with the tables to script out.
#[string] $database = "DBNAME"; # Database with the tables to script out.
#[string] $folder = "S:\DBA"; # Path to export to

Write-Output ((Get-Date -format yyyy-MM-dd_HH-mm-ss) + ": *** LinkedServers Started ***");

$folder=(Join-Path (Join-Path (Join-Path $folder $server) $serverinstance) "_ServerObjects")
if(!(Test-Path $folder)) {New-Item -ItemType directory -Path $folder}

foreach($linkedServer in (Get-ChildItem SQLSERVER:\SQL\$server\$Serverinstance\LinkedServers))
{
$linkedServer.Script() | Out-File (Join-Path $folder ($linkedServer.Name.Replace(".","_").Replace("\","$") + ".sql"))
}

Write-Output ((Get-Date -format yyyy-MM-dd_HH-mm-ss) + ": !!! LinkedServers Finished !!!");
Write-Output " "


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
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