Powershell: Setting SASS_BINARY_PATH - powershell

I need to set SASS_BINARY_PATH environment variable with the local file I've downloaded to be able to install node-sass behind a corporate firewall. So on windows cmd, I just do:
SET SASS_BINARY_PATH=C:\Source\Repos\SRT\Srt.Web\sass-binary\v4.7.2\win32-x64-48_binding.node
And the installation works fine since it successfully sets the variable. But when I try doing it via Powershell, it doesn't work:
$env:SASS_BINARY_PATH="C:\Source\Repos\SRT\Srt.Web\sass-binary\v4.7.2\win32-x64-48_binding.node"
I've also tried another way on Powershell:
[Environment]::SetEnvironmentVariable("SASS_BINARY_PATH", "C:\Source\Repos\SRT\Srt.Web\sass-binary\v4.7.2\win32-x64-48_binding.node", "Machine")
Upon checking it on the control panel, it successfully added a "SASS_BINARY_PATH" system variable. But upon trying to reinstall node-sass, it fails again.
One of my observations is when I'm doing it the windows cmd way then check it by using the command line set, the variable shows up along with others. But when I use both the Powershell methods, it does not show up. Any ideas on this?
The error encountered when trying to npm-install node-sass over a corporate firewall is:
Downloading binary from
https://github.com/sass/node-sass/releases/download/v4.7
.2/win32-x64-48_binding.node Cannot download
"https://github.com/sass/node-sass/releases/download/v4.7.2/win3
2-x64-48_binding.node":
HTTP error 401 Unauthorized

Download win32-x64-48_binding.node manually
Put it in C:\Users\<user>\AppData\Roaming\npm-cache\node-sass\4.7.2 folder.
Then try to run npm install node-sass
here is the PowerShell command #jengfad used based on above solution which is commented in the discussion
$cacheSassPath = $env:APPDATA + '\npm-cache\node-sass'
if( -Not (Test-Path -Path $cacheSassPath ) )
{
Write-Host "cacheSassPath not exists"
New-Item -ItemType directory -Path $cacheSassPath
Write-Host "cacheSassPath CREATED"
}
<# Ensure has no content #>
Get-ChildItem -Path $cacheSassPath -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName }
<# Copy local sass binary (~Srt.Web\sass-binary\4.7.2) file to cache folder #>
$sassBinaryPath = split-path -parent $MyInvocation.MyCommand.Definition
$sassBinaryPath = $sassBinaryPath + "\sass-binary\4.7.2"
Copy-Item -Path $sassBinaryPath -Recurse -Destination $npmcachedir -Container
Write-Host "node-sass binary file successfully copied!"

Related

Powershell copy all folders and files with certain extension

I have one package on my Windows machine and another package on a remote server.
The first one is -> C:\Users\One. It contains the following files:
adapter.jsx
result.js
system.jsx
moment.js
readme.txt
package called info that contains two files -> logger.jsx and date.js.
Another one is a remote target directory -> /mnt/media/Two. It is currently empty. The user and host for it are: $userAndHost = "user#foo.bar"
I want to copy all the packages and files of extensions .jsx and .js from package One to package Two. It's required to use scp here since this is a copy between two different platforms.
What I tried:
get all the items within the package:
Get-ChildItem -Path "C:\Users\One" -Recurse
filter items by certain extension, in my case they are .jsx and .js:
Get-ChildItem -Path "C:\Users\One" -Recurse | Where-Object {$_.extension -in ".js",".jsx"}
do the secure copy (scp) - I didn't come up with the script here.
Please, help me finish the script.
Hi i think you need something like this.
I write a code for you, tested working.
#Set execution policy to bypass
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
#install Posh-SSH from powershell gallery
#https://www.powershellgallery.com/packages/Posh-SSH/2.0.2
Install-Module -Name Posh-SSH -RequiredVersion 2.0.2
#import module
Import-Module Posh-SSH
#get all the items within the package in the path:
$path = 'C:\Users\One'
$items = (Get-ChildItem -Path $path -Name -File -Include ( '*.jsx', '*.js') -Recurse)
#Need destination credential
$credential = Get-Credential
#copy selected items to destination via scp
$items | ForEach-Object {
Set-SCPFile -ComputerName 'SCP-SERVER-HOST-HERE' -Credential $credential -RemotePath '/mnt/media/Two' -LocalFile "$path\$_"
}
Hope this helps you

Test-Path Remove-Item issue

I have a powershell script to install a very large application (15gb source media) from a location its been delivered to on the C drive.
At the end of the script, to ensure that the software is installed I perform a test-path of the HKLM Microsoft Windows CurrentVersion Uninstall path for the GUID, and if successful, clear the source media from the C drive.
If (Test-Path("HKLM:pathname")) { Remove-Item $path -force -recurse }
The problem I have is that the above command works via Powershell ISE when run individually. It knows the key exists so should perform Remove-Item. When run as a script, or via a deployment mechanism, it will not remove the folder.
I have even gone further and used:
GCI $Path -Recurse | Remove-Item -force -recurse
... to no avail.
Prior to introducing the Test-Path, I only had the Remove-Item $Path -force -recurse and this worked!!
So despite Test-Path correctly judging, it appears to prevent Remove-Item from doing anything. (I wrote to a log file to check the If routing)
Any thoughts? Sorry for any typos, I did not copy / paste any part of the script.
If you can't delete the key immediately after, that probably means it has a write lock that's been applied by the Test-Path cmdlet.
Try finding out if a lock exist using Sysinternals Handle command and then release it using the handle.exe -c argument, referencing the hexadecimal number.
Make sure the format used in $path matches the format used by handle.exe
$lock = & handle.exe -nobanner -a -p ($PID) ($path)
if (-not ($lock -like '*No matching handles found*')){
& handle.exe -nobanner -p ($PID) -c ($lock[0].split(':')[0].Trim(' ')) -y
}
This will only work if you have the permission to close handles.

"PowerShell on Target Machines" task fails with an error in TFS 2017\Azure Dev Ops

I am trying to run a PowerShell script present on one of the azure server using the "PowerShell on Target Machines" Task in my TFS build definition, but the task fails with the below error.
System.Management.Automation.RuntimeException: The running command
stopped because the preference variable "ErrorActionPreference" or
common parameter is set to Stop: The specified path, file name, or
both are too long. The fully qualified file name must be less than 260
characters, and the directory name must be less than 248 characters.
--->
I have copied the script in F drive but it still gives path too long error, not able to find any solution for the same.
Does anyone know what would be the reason?
Added script code as well for reference,
GetLatestDebugOutput.ps1
$DebugBuildOutput = "F:\Drops\econNextGen\SecurityScan\19.0"
$Dest = "F:\Drops\econNextGen\SecurityScan\Debug Build Output"
Remove-Item "$Dest\*" -Recurse -Force
#Code to Copy Common-App Debug Build
$Dir= $DebugBuildOutput + "\econNextGen-Common-App-Debug\"
$Latest = Get-ChildItem -Path $Dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$FolderPath= $Dir +$Latest.Name
Copy-Item -Path $FolderPath $Dest –Recurse -force
#Code to Copy Main-App Debug Build
$Dir= $DebugBuildOutput + "\econNextGen-MAIN-APP-Debug\"
$Latest = Get-ChildItem -Path $Dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$FolderPath= $Dir +$Latest.Name
Copy-Item -Path $FolderPath $Dest –Recurse -force
First suggest you directly RDP to remote target machine and check if you are able to run the same script in it. This will narrow down if the issue related to your tfs build definition and environment.
For environment, make sure you have met all prerequisites of this PowerShell on Target Machines task. And you have qualified powershell version installed.
Actually the error message is pretty straight forward, and so is the key point you should pay attention to: make sure you're not using paths that are too long or using an invalidly path. If apply for all folder and files.
Besides, try starting the build with diagnostics\debug enabled with system.debug=true and see if you can get any meaningful output for future troubleshooting.

Remove-Item : Cannot find path '\\footfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does not exist

I am trying to remove all files and folders in a specific network path location (the tfs build drop folder of one of my builds).
In my post-build script I tried the following lines: (with same results)
$Destination = '\\footfs32\Builds\TestBuildTest'
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
Remove-Item \\footfs32\Builds\TestBuildTest -Force -Recurse
Remove-Item "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
Remove-Item -LiteralPath "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
All included folders are being removed, but when it tries to remove the files in the dir, the following error pops ups:
Remove-Item : Cannot find path
'\abctfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does
not exist.'
Why is this error popping up on files only, while all folders are deleted correctly? This doesn't make any sense to me. And how do I fix it?
According to the error info.Very likely the file had been deleted in your build process.
Some events triggered during your build process, and that file had been deleted. When running the powershell script, those files couldn't be found. The Remove-item command Can not delete a file twice.
Please double check your build definition, build process and drop folder.
Please try the following command:
Remove-Item -Path "\\footfs32\Builds\TestBuildTest\*.*" -Force -Recurse

PowerShell: Using $env:userprofile in an 'IF' statement

I am using PowerShell ISE (I think 4).
I am writing logon scripts to replace the old '*.BAT' files.
I am trying to test for a user-profile condition before 'creating/deleting' certain directories from the desktop.
Example
If(($env:userprofile = "rmullins"))
{
Remove-Item $env:userprofile\Desktop\ITFILES -Recurse -Force
}
So I run the following to see what's going on:
md -Path $env:userprofile\Desktop\ITFILES
The path is created in the following location:
C:\Windows\System32.........
The MD command above works fine until I run that 'IF' statement. I think I might not understand how the $env:userprofile part works.
Any ideas?
On Windows 7:
[PS]> echo $ENV:UserProfile
C:\Users\arco444
This returns the path to the profile directory. Therefore I'd expect looking only for the username to fail the condition. I'd do a simple match instead:
if ($env:userprofile -imatch "rmullins")
{
Remove-Item $env:userprofile\Desktop\ITFILES -Recurse -Force
}