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?