# create a new shortcut $shell = New-Object-ComObject WScript.Shell $scut = $shell.CreateShortcut($shortcutPath) # launch the script with powershell.exe: $scut.TargetPath = "powershell.exe" # skip profile scripts and enable execution policy for this one call # IMPORTANT: specify only the script file name, not the complete path $scut.Arguments = "-noprofile -executionpolicy bypass -file ""$filename""" # IMPORTANT: leave the working directory empty. This way, the # shortcut uses relative paths $scut.WorkingDirectory = "" # optinally specify a nice icon $scut.IconLocation = "$env:windir\system32\shell32.dll,162" # save shortcut file $scut.Save()
# open shortcut file in File Explorer explorer.exe "/select,$shortcutPath"
PS C:\> Split-File-Path'C:\movies\Woman tries putting gas in a Tesla.mp4'-PartSizeBytes10MB -AddSelfExtractor-Verbose VERBOSE: saving to C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.0.part... VERBOSE: saving to C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.1.part... VERBOSE: saving to C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.2.part... VERBOSE: Adding extractor scripts...
Mode LastWriteTime Length Name --------------------------- -a----03.03.201918:112004 Extract Woman tries putting gas in a Tesla.mp4.lnk -a----03.03.201916:5424081750 Woman tries putting gas in a Tesla.mp4 -a----03.03.201918:1110485760 Woman tries putting gas in a Tesla.mp4.0.part -a----03.03.201918:1110485760 Woman tries putting gas in a Tesla.mp4.1.part -a----03.03.201918:113110230 Woman tries putting gas in a Tesla.mp4.2.part -a----03.03.201918:113179 Woman tries putting gas in a Tesla.mp4.3.part.ps1
PS> Join-File-Path'C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4'-Verbose-DeletePartFiles VERBOSE: processing C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.0.part... VERBOSE: processing C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.1.part... VERBOSE: processing C:\users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.2.part... VERBOSE: Deleting part files...
Mode LastWriteTime Length Name --------------------------- -a----03.03.201916:256291456 Woman tries putting gas in a Tesla.mp4.00.part -a----03.03.201916:256291456 Woman tries putting gas in a Tesla.mp4.01.part -a----03.03.201916:256291456 Woman tries putting gas in a Tesla.mp4.02.part -a----03.03.201916:255207382 Woman tries putting gas in a Tesla.mp4.03.part
PS C:\> Join-File-Path"C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4"-DeletePartFiles-Verbose VERBOSE: processing C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.00.part... VERBOSE: processing C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.01.part... VERBOSE: processing C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.02.part... VERBOSE: processing C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.03.part... VERBOSE: Deleting part files...
try { # get the file parts $files = Get-ChildItem-Path"$Path.*.part" | # sort by part Sort-Object-Property { # get the part number which is the "extension" of the # file name without extension $baseName = [IO.Path]::GetFileNameWithoutExtension($_.Name) $part = [IO.Path]::GetExtension($baseName) if ($part-ne$null-and$part-ne'') { $part = $part.Substring(1) } [int]$part } # append part content to file $writer = [IO.File]::OpenWrite($Path) $files | ForEach-Object { Write-Verbose"processing $_..." $bytes = [IO.File]::ReadAllBytes($_) $writer.Write($bytes, 0, $bytes.Length) } $writer.Close()
if ($DeletePartFiles) { Write-Verbose"Deleting part files..." $files | Remove-Item } } catch { throw"Unable to join part files: $_" } }
PS> Split-File-Path"C:\Users\tobwe\Downloads\Woman putting gas in Tesla.mp4"-PartSizeBytes6MB -Verbose VERBOSE: saving to C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.00.part... VERBOSE: saving to C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.01.part... VERBOSE: saving to C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.02.part... VERBOSE: saving to C:\Users\tobwe\Downloads\Woman tries putting gas in a Tesla.mp4.03.part...
try { # get the path parts to construct the individual part # file names: $fullBaseName = [IO.Path]::GetFileName($Path) $baseName = [IO.Path]::GetFileNameWithoutExtension($Path) $parentFolder = [IO.Path]::GetDirectoryName($Path) $extension = [IO.Path]::GetExtension($Path)
# get the original file size and calculate the # number of required parts: $originalFile = New-Object System.IO.FileInfo($Path) $totalChunks = [int]($originalFile.Length / $PartSizeBytes) + 1 $digitCount = [int][Math]::Log10($totalChunks) + 1
# read the original file and split into chunks: $reader = [IO.File]::OpenRead($Path) $count = 0 $buffer = New-Object Byte[] $PartSizeBytes $moreData = $true
# read chunks until there is no more data while($moreData) { # read a chunk $bytesRead = $reader.Read($buffer, 0, $buffer.Length) # create the filename for the chunk file $chunkFileName = "$parentFolder\$fullBaseName.{0:D$digitCount}.part"-f$count Write-Verbose"saving to $chunkFileName..." $output = $buffer
# did we read less than the expected bytes? if ($bytesRead-ne$buffer.Length) { # yes, so there is no more data $moreData = $false # shrink the output array to the number of bytes # actually read: $output = New-Object Byte[] $bytesRead [Array]::Copy($buffer, $output, $bytesRead) } # save the read bytes in a new part file [IO.File]::WriteAllBytes($chunkFileName, $output) # increment the part counter ++$count } # done, close reader $reader.Close() } catch { throw"Unable to split file ${Path}: $_" } }
param ( # the email address to send to [Parameter(Mandatory=$true, Position=0, HelpMessage='The email address to send the mail to')] [String] $Recipient,
# the subject line [Parameter(Mandatory=$true, HelpMessage='The subject line')] [String] $Subject,
# the building text [Parameter(Mandatory=$true, HelpMessage='The building text')] [String] $building,
# a valid file path to the attachment file (optional) [Parameter(Mandatory=$false)] [System.String] $FilePath = '',
PS> Send-OutlookMail-Recipient frank@test.com -Subject'Hi Frank!'-building'Trying a new PS script. See attachment.'-FilePath'c:\stuff\sample.zip'-Importance0
假设您安装了 Outlook 并且设置了用户配置文件,这行代码将在一个对话框窗口中打开写好的邮件,这样您可以再次确认并做最终修改,然后按下“发送”按钮将邮件发送出去。
如果您指定了 -SendImmediately 开关参数,PowerShell 将会试图立即发送邮件。是否能够正确发送取决于您的 Outlook 关于自动操作的安全设置。自动发送邮件可能被禁用,或是会弹出一个对话框来征得您的同意。 z