Skip to content

Commit a5b213d

Browse files
committed
custom no subscription file upload commands
1 parent 5dea32a commit a5b213d

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
2+
# ----------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# Code generated by Microsoft (R) AutoRest Code Generator.Changes may cause incorrect behavior and will be lost if the code
14+
# is regenerated.
15+
# ----------------------------------------------------------------------------------
16+
17+
<#
18+
.Synopsis
19+
Creates and uploads a new file under a workspace for the specified subscription.
20+
.Description
21+
Creates and uploads a new file under a workspace for the specified subscription.
22+
.Example
23+
{{ Add code here }}
24+
.Example
25+
{{ Add code here }}
26+
27+
.Outputs
28+
Microsoft.Azure.PowerShell.Cmdlets.Support.Models.IFileDetails
29+
.Link
30+
https://learn.microsoft.com/powershell/module/az.support/new-azsupportfile
31+
#>
32+
function New-AzSupportFileAndUploadNoSubscription {
33+
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Support.Models.IFileDetails])]
34+
[CmdletBinding(DefaultParameterSetName='CreateExpanded', PositionalBinding=$false, SupportsShouldProcess, ConfirmImpact='Medium')]
35+
param(
36+
[Parameter(Mandatory)]
37+
[Alias('FileName')]
38+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Path')]
39+
[System.String]
40+
# File name.
41+
${Name},
42+
43+
[Parameter(Mandatory)]
44+
[Alias('FileWorkspaceName')]
45+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Path')]
46+
[System.String]
47+
# File workspace name.
48+
${WorkspaceName},
49+
50+
# [Parameter(Mandatory)]
51+
# [Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Body')]
52+
# [System.Single]
53+
# # Size of each chunk
54+
# ${ChunkSize},
55+
56+
# [Parameter(Mandatory)]
57+
# [Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Body')]
58+
# [System.Single]
59+
# # Size of the file to be uploaded
60+
# ${FileSize},
61+
62+
# [Parameter(Mandatory)]
63+
# [Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Body')]
64+
# [System.Single]
65+
# # Number of chunks to be uploaded
66+
# ${NumberOfChunk},
67+
68+
[Parameter(Mandatory)]
69+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Body')]
70+
[System.String]
71+
# Path of the file to be uploaded
72+
${FilePath},
73+
74+
[Parameter()]
75+
[Alias('AzureRMContext', 'AzureCredential')]
76+
[ValidateNotNull()]
77+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Azure')]
78+
[System.Management.Automation.PSObject]
79+
# The DefaultProfile parameter is not functional.
80+
# Use the SubscriptionId parameter when available if executing the cmdlet against a different subscription.
81+
${DefaultProfile},
82+
83+
[Parameter(DontShow)]
84+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Runtime')]
85+
[System.Management.Automation.SwitchParameter]
86+
# Wait for .NET debugger to attach
87+
${Break},
88+
89+
[Parameter(DontShow)]
90+
[ValidateNotNull()]
91+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Runtime')]
92+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Runtime.SendAsyncStep[]]
93+
# SendAsync Pipeline Steps to be appended to the front of the pipeline
94+
${HttpPipelineAppend},
95+
96+
[Parameter(DontShow)]
97+
[ValidateNotNull()]
98+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Runtime')]
99+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Runtime.SendAsyncStep[]]
100+
# SendAsync Pipeline Steps to be prepended to the front of the pipeline
101+
${HttpPipelinePrepend},
102+
103+
[Parameter(DontShow)]
104+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Runtime')]
105+
[System.Uri]
106+
# The URI for the proxy server to use
107+
${Proxy},
108+
109+
[Parameter(DontShow)]
110+
[ValidateNotNull()]
111+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Runtime')]
112+
[System.Management.Automation.PSCredential]
113+
# Credentials for a proxy server to use for the remote call
114+
${ProxyCredential},
115+
116+
[Parameter(DontShow)]
117+
[Microsoft.Azure.PowerShell.Cmdlets.Support.Category('Runtime')]
118+
[System.Management.Automation.SwitchParameter]
119+
# Use the default credentials for the proxy
120+
${ProxyUseDefaultCredentials}
121+
)
122+
123+
124+
process {
125+
Write-Output "file path: " $FilePath
126+
$MaxChunkSize = 2.5 * 1024 * 1024 #2.5 MB
127+
$MaxFileSize = 5 * 1024 * 1024 #5 MB
128+
$FileContentBytes = Get-Content -Path $FilePath -Raw
129+
$FileContentByteArray = [System.Text.Encoding]::UTF8.GetBytes($FileContentBytes)
130+
$FileSize = $FileContentByteArray.Length
131+
if($FileSize -gt $MaxFileSize){
132+
throw "File size is greater than the maximum file size of 5 MB"
133+
}
134+
$ChunkSize = If($FileSize -gt $MaxChunkSize) {$MaxChunkSize} Else {$FileSize}
135+
Write-Output "Length of byte array: " $FileSize
136+
Write-Output "Max chunk size: " $MaxChunkSize
137+
Write-Output "Chunk Size: " $ChunkSize
138+
$NumberOfChunks = [int][Math]::Floor($FileSize / $ChunkSize);
139+
If($FileSize % $ChunkSize -gt 0)
140+
{
141+
$NumberOfChunks++
142+
}
143+
Write-Output "Number of chunks: " $NumberOfChunks
144+
145+
New-AzSupportFilesNoSubscription -Name $Name -WorkspaceName $WorkspaceName -FileSize $FileSize -ChunkSize $ChunkSize -NumberOfChunk $NumberOfChunks
146+
147+
Write-Output "successfully created file"
148+
$chunkIndex = 0
149+
$startIndex = 0
150+
$endIndex = $ChunkSize - 1
151+
152+
# $FileContent = [convert]::ToBase64String((Get-Content -path $FilePath -Encoding byte))
153+
154+
while($chunkIndex -lt $NumberOfChunks){
155+
Write-Output "chunk index: " + $chunkIndex
156+
Write-Output "start index: " + $startIndex
157+
Write-Output "end index: " + $endIndex
158+
$FileContent = [convert]::ToBase64String($FileContentByteArray[$startIndex..$endIndex])
159+
160+
Invoke-AzSupportUploadFilesoSubscription -FileName $Name -FileWorkspaceName $WorkspaceName -ChunkIndex $chunkIndex -Content $FileContent
161+
$chunkIndex++
162+
$startIndex = $endIndex + 1
163+
$endIndex = $FileSize - 1
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)