Powershell Execution different in Process to Script Console/Local

Hi all,

I’m seeing some strange behaviour when running Powershell/Azure Powershell commands during a deployment process. For example when using script console or local to execute Get-ChildItem it returns:
image

However, during a deployment process the same command results are output as follows:

Do you have any suggestions as to why these differ?

P.S On a side note how does one put a newline in a string to Write-Host? Backtick n does not eame to work Write-Host "nHelloWorld`n"

Thanks in advance for any help/insight you can give

Hey @steven.cooney,

Thanks for reaching out.

I’ve run Get-ChildItem in a deployment and I have the expected output.

However, I am on 2020.6.4722.

Which version are you currently running?

Would you be able to give me a screenshot of your step along with the full script you’re running in the script step?

Please feel free to DM the above to me if there is anything private in it.

Best,
Jeremy

Hi @jeremy.miller,

Thanks for the quick response.

I just updated Octopus Server this morning to v2020.6 (Build 4915) although it was the same behaviour on the previous version we were running (I don’t have that recorded). We are also running Octopus on Windows Server Core.

The step is an Azure Powershell step however, I’m seeing the same with just Powershell


The simple script is below (forgive the write-host commands I cannot seem figure out newline characters in strings)

Write-Host “Move to Root”
Set-Location “…/…/”

Write-Host
Write-Host
Write-Host “List Files:”
Get-ChildItem
Write-Host
Write-Host

Cheers,
Steven

Hey Steven,

Thanks for all of the information.

Are you trying to get the directory information for C:\Octopus\Work\################ or C:\Octopus? If you are trying to get the former, you don’t need to do anything extra other than Get-ChildItem. If you are trying to get the latter, you do cd .. 2 times rather than the Set-Location command.

Can you give either of those a try depending on what your use case is and let me know if it works?

Regarding the newline, I believe we trim any blank lines when writing to the log to preserve space. In my testing, Write-Host " " (with a space in between) worked for me, but it does leave a space character in your log. I’m not sure if that will mess up any external parsing you have setup or if this is purely for aesthetics.

Please let me know what you think, and how the testing on your Get-ChildItem goes.

Best,
Jeremy

Hey Jeremy,

Thanks for the tip for Write-Host. Do you know why Write-Host "<BACKTICK>n" doesn’t work for a new line in Octopus when it does for native powershell?

The moving of location was to ensure I was in the referenced package when I run Get-ChildItems but what I’m really looking for is why my output for Get-ChildItem is different to yours (which is what I was hoping for)?

Thanks for you assistance,
Steven

Hey Steven,

You’re very welcome!

Thanks for the tip for Write-Host. Do you know why Write-Host “n” doesn’t work for a new line in Octopus when it does for native powershell?

This is due to how Octopus does its logging. To save space, we get rid of newlines. It technically is working inside the PowerShell instance that’s running the deployment, but the newline is not getting transferred to the log.

The moving of location was to ensure I was in the referenced package when I run Get-ChildItems but what I’m really looking for is why my output for Get-ChildItem is different to yours (which is what I was hoping for)?

When you do just Get-ChildItem with no Set-Location, does it output the work directory correctly?

If so, we can change the directory using system variables to the directory of the extracted package.

You can try with:

cd "#{Octopus.Action[StepName_Where_The_Package_Got_Deployed].Output.Octopus.Action.Package.InstallationDirectoryPath}"
Get-ChildItem

Can you please let me know if I explained that okay and if it works for you?

Best,
Jeremy

Hi Jeremy,

I have run a script similar to yours but the output for the Get-ChildItem is the same.

Write-Host $OctopusParameters[“Octopus.Action.Package[MyPackage].ExtractedPath”]
cd $OctopusParameters[“Octopus.Action.Package[MyPackage].ExtractedPath”]
Get-ChildItem

I’ll PM you a screenshot of the output.

I’m not sure if itis relevent but I am running the script on the Server rather than a deployment tentacle or worker.

Thanks,
Steven

Hey Steven,

Depending on how you have your process and server/tentacle setup, it likely will not work to run that command on the server if the package is being extracted on a tentacle. You would need to run it on the tentacle to get to the path.

Can I please ask what the high level use case is you’re trying to achieve here? Maybe we can attack this from a different angle.

Best,
Jeremy

Hey Jeremy,

The deployment process should be entirely run on the server, so didn’t think tentacles would be involved.

Essentially, (all on the server) I wanted to extract the package and run Get-ChildItem on the contents. I’m less bothered about been in the correct directory to achive the list and more around the output from Get-ChildItem. I would expect an outcome like your image a path, a concise list of items within the directory, however, as you can see from my screenshot I get really verbose output. When I do the same thing in the script console the verbose output is not used, it matches your output.

Expected something like:

Actual:

Cheers,
Steven

Hi Steven,

Can you please go to our DM and also attach a copy of your deployment process JSON?

To get this go to the Project, then the process section, then click the 3 dots to the right of add step and click download JSON.

Thanks,
Jeremy

Hi Steven,

Thanks for all of the information.

I was able to reproduce your results in a Azure Script Step, but regular Script Steps worked normally.

I’m going to have a conversation with our developers regarding this, but in the meantime, depending on what you want to do with the Get-ChildItem, you may be able to do it with a workaround.

$Obj = Get-ChildItem
foreach ($item in $Obj){
Write-Host $item
}

This resulted in outputting the folder names and items within the current directory. You could implement logic to work with those items as well but I didn’t do a lot of testing beyond that as I wasn’t sure what your use case was.

Please let me know if you think that workaround will do for now while I get developer input.

Best,
Jeremy

Hi Jeremy,

Thanks for your patience in looking into this for me and I appreciate you raising it to the developers. It isn’t a major thing but one of those niggles that cause the log output to be convoluted.

I have also been using Get-ChildItem -Name as a workaround which just prints out the names of files and directories which I think will do something similar to your loop.

Cheers,
Steven

Hey Steven,

Thanks for the tip on Get-ChildItem -Name. That’s definitely a lot cleaner than my hacky solution.

I’m still trying to dig in and find out the root cause of this one and I will keep you updated.

Please feel free to reach out in the meantime.

Best,
Jeremy

Hey Steven,

Sorry about the delay on creating the issue. I was discussing this internally and doing tests to see if there was a quick way of solving this.

Here is the issue so you can subscribe for updates: Get-ChildItem in a Run an Azure Script Step returns different results than in a Run a Script Step · Issue #6877 · OctopusDeploy/Issues · GitHub

Please let me know if you have any other questions or concerns.

Best,
Jeremy

Hi Jeremy,

Thanks for submitting a ticket, I’ve subscribed for progress updates.

Cheers,
Steven

2 Likes

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.