Files
management-scripts/Create-ISOShare.ps1
T
2019-11-11 19:22:39 -05:00

117 lines
4.2 KiB
PowerShell

#Requires -Version 3.0
<#
.SYNOPSIS
This script will mount an ISO file and share it on the network.
.DESCRIPTION
The ISO file can be a local path or fetched from a URL. The share this script creates will allow 'Everyone' read-only access.
.PARAMETER ShareName
This is the name of the Windows share that will be created.
.PARAMETER ISOPath
The full absolute path to the ISO. This can be a local path or a URL.
.PARAMETER NoShare
Will prevent the script from creating a network share. It will return only the drive letter for the mounted ISO file.
.PARAMETER Persist
Will cause the ISO to remain mounted even after rebooting the endpoint.
.PARAMETER Redownload
If -ISOPath is a URL, this parameter will delete an existing ISO file with the same name and download a fresh copy.
.PARAMETER Remove
Will remove the network share, and dismount the ISO file. This will not delete the ISO file.
.INPUTS
None. This script does not accept pipeline input.
.OUTPUTS
This script will output only a string containing the status of the shared ISO file.
.EXAMPLE
.\Create-ISOShare.ps1 -ShareName 'Win10Setup' -ISOPath 'D:\path\to\windows_10_setup.iso'
.EXAMPLE
.\Create-ISOShare.ps1 -ShareName 'Win10Setup' -ISOPath 'D:\path\to\windows_10_setup.iso' -Remove
.EXAMPLE
.\Create-ISOShare.ps1 -ShareName 'Win10Setup' -ISOPath 'https://hosted.net/path/to/windows_10_setup.iso'
.EXAMPLE
.\Create-ISOShare.ps1 -ISOPath 'https://hosted.net/path/to/windows_10_setup.iso' -NoShare
.NOTES
This script requires an administrative token to run properly.
.LINK
https://www.microsoft.com/en-us/software-download/windows10
#>
param(
[string]$ShareName,
[string]$ISOPath,
[switch]$NoShare,
[switch]$Persist=$false,
[switch]$Redownload,
[switch]$Remove
)
## Set the download directory for downloaded ISO file
## Note: This is only needed if a URL is passed to -ISOPath
$DownloadDirectory = $Env:SYSTEMDRIVE
## Download the ISO if a URL is specified
If ( $ISOPath.StartsWith('http') )
{
## Get the URL into its own variable
$DownloadURL = $ISOPath
## Get the file name from the URL
$filename = Split-Path -Path $DownloadURL -Leaf
## Set the path to the soon-to-be-downloaded ISO file
$ISOPath = "${DownloadDirectory}\${filename}"
## If $Redownload is specified, delete the ISO so we can redownload it
If ( ($Redownload) -and (Test-Path $ISOPath) ) { Remove-Item $ISOPath -Force }
}
## Function to remove the share and dismount the ISO
Function Remove-ISOShare
{
## Remove the share
Remove-SMBShare -Name $ShareName -Force -ErrorAction SilentlyContinue
## Dismount the ISO file
If ( Test-Path $ISOPath ) { Dismount-DiskImage -ImagePath $ISOPath -StorageType ISO | Out-Null }
}
## Just remove the share and dismount the ISO if $Remove is specified
If ( $Remove ) { Remove-ISOShare }
## If $Remove is not specified, mount the ISO and create a network share
Else
{
## If $DownloadURL exists and the ISO doesn't, download it
If ( ($DownloadURL) -and (!(Test-Path $ISOPath)) )
{
(New-Object System.Net.WebClient).DownloadFile($DownloadURL, $ISOPath)
}
## Make sure we remove the share and mounted ISO, since both might already exist
Remove-ISOShare
## Mount the ISO and get the drive letter
$MountResult = Mount-DiskImage -ImagePath $ISOPath -StorageType ISO -Access ReadOnly -PassThru
$DriveLetter = ($MountResult | Get-Volume).DriveLetter
## Create network share
If (! ($NoShare) )
{
New-SMBShare -Name $ShareName -Path "${DriveLetter}:\" -ContinuouslyAvailable:$Persist -ReadAccess 'Everyone' | Out-Null
}
## Get the share path (or local path if it isn't shared)
If ( $NoShare ) { $SharePath = "${DriveLetter}:\" }
Else { $SharePath = "\\${Env:COMPUTERNAME}\${ShareName}" }
If ( Test-Path $SharePath )
{
## Return the full share path
Write-Output "ISO Share Path:`n`r${SharePath}"
## Return the full command to upgrade Windows 10 using this share
Write-Output "Windows 10 upgrade command:`n`r${SharePath}\setup.exe /auto upgrade /dynamicupdate disable /showoobe none /quiet"
} Else { Write-Output "ISO could not be shared." }
}