Asked  7 Months ago    Answers:  5   Viewed   110 times

I'm trying to retrieve the hash of a file located on remote server using Invoke-Command. It works fine when I give the full path as below:

Invoke-Command -ComputerName winserver -ScriptBlock { 
    Get-FileHash -Path E:testtestfile.zip -Algorithm SHA1 
}

But I need to pass the file name via a variable as below:

Invoke-Command -ComputerName winserver -ScriptBlock { 
    Get-FileHash -Path "E:test$dest.zip" -Algorithm SHA1 
}

How do I access this variable in the scriptblock of a remote session?

 Answers

34

In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:

Invoke-Command -ComputerName winserver -ScriptBlock { 
    Get-FileHash E:test$Using:dest.zip -Algorithm SHA1 
}

For a solution that works with all versions:

Invoke-Command -ComputerName winserver -ScriptBlock {
    param($myDest)

    Get-FileHash E:test$myDest.zip -Algorithm SHA1 
} -ArgumentList $dest
Tuesday, June 1, 2021
 
PandemoniumSyndicate
answered 7 Months ago
24

The best method i can think of looks like this:

<?php
$php_var = session_id();
?>
<script type="text/javascript">
    var js_var = <?php echo json_encode($php_var); ?>;
</script>

PHP's json_encode-function is always producing valid JavaScript, which is not ensured if you are simply outputting random values. If you decide not to use json_encode(), you should at least enclose the php-value with quotes to prevent syntax errors. Be aware of escaping!

<?php
$php_var = session_id();
?>
<script type="text/javascript">
   var js_var = "<?php echo $php_var; ?>";
</script>
Wednesday, March 31, 2021
 
redrom
answered 9 Months ago
80

Edit:

This answer is possibly not valid for Unity-based flavours of Ubuntu.


The issue is actually nothing to do with NetBeans - it's related to the Ubuntu (ie Gnome) Launcher.

As this blog post explains, you need to add variables to the rather obscure ~/.gnomerc (No Mercy? :) file in order for them to be passed to applications started with Launcher!

So just edit ~/.gnomerc and add the variables as you would to ~/.bashrc, eg:

export MYVAR="xyz"

and logout/login.

Friday, August 13, 2021
 
Vikram
answered 4 Months ago
95

The problem is the http://schemas.microsoft.com/developer/msbuild/2003 namespace. You need to take account of this namespace in your XPath expressions, as unprefixed element names in XPath refer to elements that are not in a namespace.

[xml]$csproj = Get-Content MyProject.csproj
$ns = new-object Xml.XmlNamespaceManager $csproj.NameTable
$ns.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003")

$refs = $csproj.SelectNodes("//msb:ProjectReference", $ns)
foreach($ref in $refs) {
  # Later on output more useful information
  Write-Host $ref.Name
}

(adapted from this answer)

Saturday, September 18, 2021
 
skrilled
answered 3 Months ago
74

Get-Content converts your file into a sting array based on those characters. By the time you start to test the contents those characters have been stripped out. If you are just looking for the presence of newlines (your condition does not seem to care what it encounters) testing the amount of lines returned from Get-Content would be enough.

If((Get-Content $masterFilename).Count -gt 1){"you betcha, there's a carriage return or line feed"}

I'm sure I will add to this once you see it. I think you might need to revise your question to be a little more specific.

Get-Content has a -Raw switch which would preserve the new line delimiters which is closer to where you want to be going. I'm not sure the string method contains supports wildcards. I do know that it does not support regex.


If you were just curious about one line then as mentioned above -Raw would be the place to start. Consider the following text file represented in hex. It is just the word hello followed by a newline.

68 65 6c 6c 6f 0d 0a

Now I will read that text in as one string and test if the end of the string has a carriage return and line feed. Note that I am not escaping with backticks. I am using regex special characters. (It would work either way. Just something to be aware of.)

(Get-Content c:temptest.txt -Raw) -match "rn$"
True

Note that most of the PowerShell cmdlets that write to file leave a trailing newline. So no matter how many lines you have the above code snippet would be true for something like Set-Content.

Monday, October 11, 2021
 
Jame
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share