Skip to content

Commit e568fda

Browse files
kitthollandlzybkr
authored andcommitted
Rewrite New-TemporaryFile in C# (PowerShell#2786)
Literal translation of New-TemporaryFile function to a C# cmdlet, other than moving logic from Begin to End.
1 parent 9aa693d commit e568fda

File tree

6 files changed

+55
-39
lines changed

6 files changed

+55
-39
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
using System;
3+
using System.IO;
4+
using System.Management.Automation;
5+
6+
namespace Microsoft.PowerShell.Commands
7+
{
8+
/// <summary>
9+
/// The implementation of the "New-TemporaryFile" cmdlet
10+
/// </summary>
11+
[Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=526726")]
12+
[OutputType(typeof(System.IO.FileInfo))]
13+
public class NewTemporaryFileCommand : Cmdlet
14+
{
15+
/// <summary>
16+
/// returns a TemporaryFile
17+
/// </summary>
18+
protected override void EndProcessing()
19+
{
20+
string filePath = null;
21+
string tempPath = System.Environment.GetEnvironmentVariable("TEMP");
22+
if (ShouldProcess(tempPath))
23+
{
24+
try
25+
{
26+
filePath = Path.GetTempFileName();
27+
}
28+
catch (Exception e)
29+
{
30+
WriteError(
31+
new ErrorRecord(
32+
e,
33+
"NewTemporaryFileWriteError",
34+
ErrorCategory.WriteError,
35+
tempPath));
36+
return;
37+
}
38+
if (!string.IsNullOrEmpty(filePath))
39+
{
40+
FileInfo file = new FileInfo(filePath);
41+
WriteObject(file);
42+
}
43+
}
44+
}
45+
}
46+
}

src/Modules/Shared/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psm1

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,37 +141,6 @@ function Get-FileHash
141141
}
142142
}
143143

144-
<# This cmdlet is used to create a new temporary file in $env:temp #>
145-
function New-TemporaryFile
146-
{
147-
[CmdletBinding(
148-
HelpURI='https://go.microsoft.com/fwlink/?LinkId=526726',
149-
SupportsShouldProcess=$true)]
150-
[OutputType([System.IO.FileInfo])]
151-
Param()
152-
153-
Begin
154-
{
155-
try
156-
{
157-
if($PSCmdlet.ShouldProcess($env:TEMP))
158-
{
159-
$tempFilePath = [System.IO.Path]::GetTempFileName()
160-
}
161-
}
162-
catch
163-
{
164-
$errorRecord = [System.Management.Automation.ErrorRecord]::new($_.Exception,"NewTemporaryFileWriteError", "WriteError", $env:TEMP)
165-
Write-Error -ErrorRecord $errorRecord
166-
return
167-
}
168-
169-
if($tempFilePath)
170-
{
171-
Get-Item $tempFilePath
172-
}
173-
}
174-
}
175144
<############################################################################################
176145
# Format-Hex cmdlet helps in displaying the Hexadecimal equivalent of the input data.
177146
############################################################################################>

src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
2222
"Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
2323
"Get-TraceSource", "Set-TraceSource", "Trace-Command",
2424
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
25-
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime"
26-
FunctionsToExport= "Get-FileHash", "New-TemporaryFile", "Format-Hex", "Import-PowerShellDataFile"
25+
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile"
26+
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile"
2727
AliasesToExport= "fhx"
2828
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"
2929
HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390787'

src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
1919
"Group-Object", "Sort-Object", "Get-Variable", "New-Variable", "Set-Variable", "Remove-Variable",
2020
"Clear-Variable", "Export-Clixml", "Import-Clixml", "ConvertTo-Xml", "Select-Xml", "Write-Debug",
2121
"Write-Verbose", "Write-Warning", "Write-Error", "Write-Information", "Write-Output", "Set-PSBreakpoint",
22-
"Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
22+
"Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
2323
"Get-TraceSource", "Set-TraceSource", "Trace-Command",
2424
"Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
2525
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime"
26-
FunctionsToExport= "Get-FileHash", "New-TemporaryFile", "Format-Hex", "Import-PowerShellDataFile",
27-
"ConvertFrom-SddlString"
26+
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString"
2827
AliasesToExport= "fhx"
2928
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"
3029
HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390787'

src/Modules/Windows-Full/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
2424
"Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
2525
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Show-Command", "Unblock-File",
2626
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", "Get-RunspaceDebug", "Wait-Debugger",
27-
"ConvertFrom-String", "Convert-String" , "Get-Uptime"
28-
FunctionsToExport= "Get-FileHash", "New-TemporaryFile", "Format-Hex", "Import-PowerShellDataFile",
29-
"ConvertFrom-SddlString"
27+
"ConvertFrom-String", "Convert-String" , "Get-Uptime", "New-TemporaryFile"
28+
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString"
3029
AliasesToExport= "CFS", "fhx"
3130
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"
3231
HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390787'

src/vs-csproj/Microsoft.PowerShell.Commands.Utility.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@
229229
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\commands\utility\NewGuidCommand.cs">
230230
<Link>commands\utility\NewGuidCommand.cs</Link>
231231
</Compile>
232+
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\commands\utility\NewTemporaryFileCommand.cs">
233+
<Link>commands\utility\NewTemporaryFileCommand.cs</Link>
234+
</Compile>
232235
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\commands\utility\NewTimeSpanCommand.cs">
233236
<Link>commands\utility\NewTimeSpanCommand.cs</Link>
234237
</Compile>

0 commit comments

Comments
 (0)