I have a SQL Job that (among other things) manipulates some files in folders that are named after SQL Named Instances.
Here's the relevant code within the SQL Job:
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId,
@step_name=N'Do Stuff',
@step_id=1,
blah blah blah...
@os_run_priority=0, @subsystem=N'PowerShell',
@command= $dir = "\\SomeNetworkLocation\" + $env:computername
#do stuff with $dir
So this part, $dir = "\\SomeNetworkLocation\" + $env:computername, grabs the computer name and makes the full path of my file location, which works great for most of my servers. The issue I'm having is some of my servers are Named Instances, so pulling the server name isn't good enough, as it only returns the first part of the Named Instance. I need it get $env:computername + '_' + $MyInstance somehow. The format of named instances on SQL are MyServer\MyInstance, if that helps...
Is there a way to pull the named of the instance this job exists on via PowerShell?
Get-SqlInstance -ServerInstance "MyServer"errors withA parameter cannot be found that matches parameter name 'ServerInstance'.andGet-SqlInstance -Credential -ServerInstance "TECHPCJRF03"prompts me to login, but even with logging in fails withVerify that the target server is accessible and that the SQL Server Cloud Adapter service is running.It is accessible though. I'm logged in now!$env:computernameworks), and it won't know because you could have several named instances on a particular machine. You will have to pull the named instance from the SQL script and pass it as a parameter into the PowerShell job.SELECT @@servernameto the PowerShell section of the job? The PowerShell is all contained within that@commandI posted above.