Pipeline from custom variable

Hi everybody,

I don’t find how to use pipeline from my first script for use it to my second script.

Here is my first script, Script-1 that simply assign a value :

[int]$Number = 3

And here is my second script, Script-2, that shows the value :

[cmdletbinding()]
param(
    [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
    [int]$Number
)

begin {Write-Host "Start"}

process
{
    Write-Host $Number
}

end {Write-Host "Stop"}

I’ve tested these scripts with two commands.

First command :
Input :

Script-1 | Script-2

Output :

Start
Stop

Second command:
Input:

Get-Disk | Script-2

Output :

Start
0
1
Stop

I don’t understand why with Get-Disk, Script-2 get $Number value, and not get this value with Script-1.

Thank you for your help and have a good day :slight_smile:

Your first script does not output anything. So there is nothing to be passed down the pipeline to the second script.

Thank you Olaf. Do how can I set the variables for it output the value ?

That’s a beginner question you actually learn in the first minutes of learning PowerShell. :smirk:

When you defined it you simply output it.

$Number

I’d recommend to do a big step back and start with learning the very basics of PowerShell first. This will save you from a lot of wasted time and frustrations. :wink:

Hi,

I’ve searched since yesterday how to resolve the problem, but I’ve found anything.
I’ve simplified my scripts for the post, in reality I have two scripts more difficult, a script for mount an image on a USB drive, and a scripts for eject the drive.

My script “Prepare-Disk” for mount image :

Param (
# Specifies the disk number
[Parameter(Mandatory = $false)]
[string]$Hash,

[Parameter(Mandatory = $false, ParameterSetName="DiskNumber")]
[UInt32]$DiskNumber,

[Parameter(Mandatory = $true)]
[string]$ImageFile,

[switch]$Save

)

function Compare-Hash
{
if (!$Hash)
{
return
}

switch ($Hash.Length)
{
    32  {$algorithm = "MD5"     }
    40  {$algorithm = "SHA1"    }
    64  {$algorithm = "SHA256"  }
    96  {$algorithm = "SHA384"  }
    128 {$algorithm = "SHA512"  }
}

$hashImage = (Get-FileHash $ImageFile -Algorithm $algorithm).Hash
$hashResult = $false

if ($Hash -eq $hashImage)
{
    $hashResult = $true
}

if (-not $hashResult)
{
    Write-Host "Hash error : Hash file and hash value don't match, verify image source." -ForegroundColor red
    exit
}

}

#Detect and select disk to use
function Select-Disk
{
$listDisk = Get-Disk | Where-Object BusType -eq USB
$listCountDisk = $listDisk.Number.Count

# If there is no USB drive
if ($listCountDisk = 0)
{
    Write-Host "There is no USB drive detected" -ForegroundColor red
    return
}

# If there is only one USB drive
if ($listCountDisk = 1)
{
    $global:number = $listDisk.Number
}

# If there is two or more USB drive
if ($listCountDisk -ge 2 -And -not $DiskNumber)
{
    $listDisk
    [Environment]::NewLine
    [UInt32]$global:number = Read-Host "Enter the disk number to format"
}

}

#Save files and folders in disk
function Save-Disk-Item
{
if (!$Save)
{
return
}

$listDrive = (Get-Partition -number $number).DriveLetter

foreach ($drive in $listDrive)
{
    $directoryInfo = Get-ChildItem "$($drive):\" | Measure-Object

    if ($directoryInfo.count -eq 0)
    {
        break
    }

    $listItem = (Get-ChildItem "$($drive):\").FullName
    $savePath = "$($env:UserProfile)\Documents\Save drive ($($drive))"

    New-Item -Path $savePath -ItemType Directory
    Move-Item $listItem $savePath
}

}

function Format-Disk
{
$diskName = (Get-Disk -Number $number).Manufacturer
$mountImg = Mount-DiskImage -ImagePath $ImageFile
$mountLetter = ($mountImg | Get-Volume).DriveLetter

Clear-Disk -Number $number -RemoveData -RemoveOEM -Confirm:$true -PassThru | Out-Null
New-Partition -DiskNumber $number -UseMaximumSize -IsActive -AssignDriveLetter | Out-Null

$global:diskLetter = (Get-Partition -DiskNumber $number).DriveLetter

Format-Volume -DriveLetter $diskLetter -FileSystem FAT32 -NewFileSystemLabel "$diskName" | Out-Null

Copy-Item "$($mountLetter):\*" "$($diskLetter):\"
DisMount-DiskImage -ImagePath $ImageFile | Out-Null

}

Compare-Hash
Select-Disk
Save-Disk-Item
Format-Disk

My script for “Eject-Disk” :

Param (
# Specifies the disk number
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[string]$Number
)

function Eject
{
$eject = New-Object -comObject Shell.Application
$eject.NameSpace(17).ParseName(“$($Number):”).InvokeVerb(“Eject”)
}

When I run the command :

Prepare-Disk -ImageFile “C:\Users\UserName\Downloads\debian-11.3.0-amd64-netinst.iso” | Eject-Disk

The first script works perfectly ! But the second script doesn’t eject the USB drive on which I work. I’ve search several solution, but I don’t know how to recover the disk number for use it in “Eject-Disk” and eject the USB drive after having mounting.
Thank you again.

Hi,
Good practice- use $script scope, not $global.
U should use disk letter in command below, not disk number.
$eject.NameSpace(17).ParseName(“$diskLetter”).InvokeVerb(“Eject”)