Ron.k
Ron.k

Reputation: 37

Store Hostname in a variable

When I go to PowerShell and type there:

PS> Hostname

I get the name of the current machine - which is ok (the name is: my_host) My question is, how can I store this value into a variable?

I have this query (backup DB):

$query = "BACKUP DATABASE [my_db] TO  DISK = N'\\${Hostname}\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION"

As you can see, I inserted there the variable $Hostname, but it doesn't work like that.

As I mentioned, my Hostname is my_host, so this is the expected result:

$query = "BACKUP DATABASE [my_db] TO  DISK = N'\\my_host\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION"

How can I do it?

BTW, I know that it doesn't work, because when I run this command, it fails:

Invoke-Sqlcmd -Username user -Password 12345 -ErrorAction Stop -ServerInstance my-db-server -Database master -Query ${query} -QueryTimeout 0 -OutputSqlErrors 1

Upvotes: 3

Views: 18839

Answers (4)

will
will

Reputation: 11

this is the shortest way I could figure out:

$hostname = (hostname)

Upvotes: 1

vrdse
vrdse

Reputation: 3154

hostname is actually hostname.exe, i.e. it's not a native command that is interpreted by the shell, but an external command that is invoked.

You can assign the output to an variable as usual:

$hostname = hostname.exe

Then you can build the query string like this

$query ="BACKUP DATABASE [my_db] TO  DISK = N'\\$hostname\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION"

or with the -f operator

$query = "BACKUP DATABASE [my_db] TO  DISK = N'\\{0}\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION" -f $Hostname

If you want to use hostname.exe without a helper variable you can use a subexpression $().

$query = "BACKUP DATABASE [my_db] TO  DISK = N'\\$(hostname.exe)\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION"

Your example with ${hostname} equals $hostname which is an empty variable and does not call hostname.exe.

However, there are various ways to get the hostname without calling an external command, for instance:

Environment variable: $env:COMPUTERNAME

WMI Class: Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty Name

You can use both examples as shown before.

Upvotes: 3

G42
G42

Reputation: 10019

On windows, you could use the native PowerShell environment variable $env:COMPUTERNAME. So one way is:

$query ="BACKUP DATABASE [my_db] TO  DISK = N'\\${$env:COMPUTERNAME}\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION"

Sidenote - -Query ${query} can be simplified to -Query $query. Curly brackets make sense when you would refer to an incorrect variable otherwise. Example below.

$a = "Easy as "
Write-Host "$aabc"    # looks for variable $aabc
Write-Host "${a}abc"  # looks for variable $a, then prints abc

Upvotes: 0

Veikko
Veikko

Reputation: 3610

You can use the Hostname command in your variable with syntax $(Hostname). Your command will work like you expect in this format:

$query ="BACKUP DATABASE [my_db] TO  DISK = N'\\$(Hostname)\Backup\test\DB\my_db.bak' WITH NOFORMAT, NOINIT,  NAME = N'my_db Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION"

Upvotes: 2

Related Questions