Hi,
I am trying to make a flexible script to get the Service Account Name from several servers.
I am using "servername.master.dbo.xp_instance_regread.......", but I want to use a variable for the servername.
So I started doing this:
Declare @servername varchar(100)
Declare @SQL varchar(1000)
Declare @agtServAccName varchar(250)
set @servername = 'Server1'
set @sql = @servername + '.master.dbo.xp_instance_regread
N''HKEY_LOCAL_MACHINE'',
N""SYSTEM\CurrentControlSet\Services\SQLSERVERAGENT'',
N''ObjectName'',
@AgtServAccName Output,
N''no_output'''
Execute (@SQL) --(This runs without problems)
Select @AgtServAccName -- <-- This gives an error "Must declare variable @AgtServAccName'
So the variable @AgtServAccName is 'forgotten' after the Execute statement...
How am I able to get a flexible script, which gives me the Service Account Name?
EDIT: the reason I use a variable for the servicename is, that I am running it on a central management server and not on the server with this servername itself.