Skip to content

Commit

Permalink
Add powershell support in VM-Install-Shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
emtuls committed Jan 26, 2024
1 parent 79c35a7 commit c8655cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/common.vm/common.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>common.vm</id>
<version>0.0.0.20240119</version>
<version>0.0.0.20240125</version>
<description>Common libraries for VM-packages</description>
<authors>Mandiant</authors>
</metadata>
Expand Down
41 changes: 17 additions & 24 deletions packages/common.vm/tools/vm.common/vm.common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,11 @@ function VM-Install-Raw-GitHub-Repo {
}

if ($powershellCommand) {
$executableArgs = "-ExecutionPolicy Bypass -NoExit -Command $powershellCommand"
$powershellPath = Join-Path "${Env:WinDir}\system32\WindowsPowerShell\v1.0" "powershell.exe" -Resolve
$shortcut = Join-Path $shortcutDir "$toolName.lnk"
Install-ChocolateyShortcut -shortcutFilePath $shortcut -targetPath $powershellPath -Arguments $executableArgs -WorkingDirectory $toolDir -IconLocation $powershell
VM-Install-Shortcut -toolName $toolName -category $category -executablePath $powershellPath -arguments $powershellCommand -executableDir $toolDir -powershell $true
} else {
$shortcut = Join-Path $shortcutDir "$toolName.lnk"
Install-ChocolateyShortcut -shortcutFilePath $shortcut -targetPath $toolDir
VM-Install-Shortcut -toolName $toolName -category $category -executablePath $toolDir
}
VM-Assert-Path $shortcut

return $toolDir
} catch {
Expand All @@ -271,6 +267,8 @@ function VM-Install-Shortcut{
[Parameter(Mandatory=$false)]
[bool] $consoleApp=$false,
[Parameter(Mandatory=$false)]
[bool] $powershell=$false,
[Parameter(Mandatory=$false)]
[switch] $runAsAdmin,
[Parameter(Mandatory=$false)]
[string] $executableDir,
Expand All @@ -284,15 +282,20 @@ function VM-Install-Shortcut{

if (-Not $iconLocation) {$iconLocation = $executablePath}

if ($consoleApp) {
if (!$executableDir) {
$executableDir = Join-Path ${Env:UserProfile} "Desktop"
if ($consoleApp -or $powershell) {
if (-not $executableDir) {
$executableDir = Join-Path ${Env:UserProfile} "Desktop"
}
VM-Assert-Path $executableDir

$executableCmd = Join-Path ${Env:WinDir} "system32\cmd.exe" -Resolve
# Change to executable dir, print command to execute, and execute command
$executableArgs = "/K `"cd `"$executableDir`" && echo $executableDir^> $executablePath $arguments && `"$executablePath`" $arguments`""
if ($consoleApp) {
$executableCmd = Join-Path ${Env:WinDir} "system32\cmd.exe" -Resolve
# Change to executable dir, print command to execute, and execute command
$executableArgs = "/K `"cd `"$executableDir`" && echo $executableDir^> $executablePath $arguments && `"$executablePath`" $arguments`""
} else {
$executableCmd = "powershell.exe"
$executableArgs = "-ExecutionPolicy Bypass -NoExit -Command `"`$cmd = '$arguments'; Write-Host `$cmd; Invoke-Expression `$cmd`""
}

$shortcutArgs = @{
ShortcutFilePath = $shortcut
Expand Down Expand Up @@ -460,7 +463,6 @@ function VM-Install-Single-Ps1 {
)
try {
$toolDir = Join-Path ${Env:RAW_TOOLS_DIR} $toolName
$shortcutDir = Join-Path ${Env:TOOL_LIST_DIR} $category

# Download and install
$scriptPath = Join-Path $toolDir "$toolName.ps1"
Expand All @@ -477,18 +479,9 @@ function VM-Install-Single-Ps1 {
Get-ChocolateyWebFile @packageArgs
VM-Assert-Path $scriptPath

$shortcut = Join-Path $shortcutDir "$toolName.lnk"
$targetCmd = Join-Path ${Env:WinDir} "system32\cmd.exe" -Resolve

if ($ps1Cmd) {
$targetArgs = "/K powershell.exe -ExecutionPolicy Bypass -NoExit -Command `"cd '$toolDir'; $ps1Cmd`""
} else {
$targetArgs = "/K powershell.exe -ExecutionPolicy Bypass -NoExit -Command `"cd '$toolDir'`""
}
$targetIcon = Join-Path (Join-Path ${Env:WinDir} "system32\WindowsPowerShell\v1.0") "powershell.exe" -Resolve
$powershellPath = Join-Path (Join-Path ${Env:WinDir} "system32\WindowsPowerShell\v1.0") "powershell.exe" -Resolve
VM-Install-Shortcut -toolName $toolName -category $category -executablePath $powershellPath -executableDir $toolDir -powershell $true -arguments $ps1Cmd

Install-ChocolateyShortcut -shortcutFilePath $shortcut -targetPath $targetCmd -arguments $targetArgs -workingDirectory $toolDir -iconLocation $targetIcon
VM-Assert-Path $shortcut
return $scriptPath
} catch {
VM-Write-Log-Exception $_
Expand Down

0 comments on commit c8655cf

Please sign in to comment.