我制作了一个简单的 powershell 脚本,它有两个参数。如果我尝试不带参数执行脚本,它工作正常。如果我尝试使用两个参数之一执行它,它工作正常。当我尝试使用两个参数运行脚本时,出现以下错误:
Parameter set cannot be resolved using the specified named parameters.
Run Code Online (Sandbox Code Playgroud)
它还有一个关于 AmbiguousParameterSet 的花絮。
我的脚本的参数部分如下所示:
[CmdletBinding(DefaultParameterSetName="None")]
param(
[Parameter(Mandatory=$False, ParameterSetName="BuildExclude")]
[ValidateSet('proj1','proj2', 'proj3', 'proj4')]
[string[]]$BuildExclude,
[Parameter(Mandatory=$False, ParameterSetName="SkipDbRestore")]
[switch]$SkipDbRestore
)
Run Code Online (Sandbox Code Playgroud)
这些用法有效:
.\RunScript.ps1 -BuildExclude proj1, proj2
.\RunScript.ps1 -SkipDbRestore
.\RunScript.ps1
Run Code Online (Sandbox Code Playgroud)
由于上述原因,此用法不起作用:
.RunScript.ps1 -BuildExclude proj1, proj2 -SkipDbRestore
Run Code Online (Sandbox Code Playgroud)
即使我颠倒了参数的顺序,它也不起作用。我尝试添加参数位置,使 BuildExclude=0 和 SkipDbRestore=1 ,希望如果我至少保持它们的顺序,我就能让它们一起工作。但这给了我同样的歧义错误。我错过了什么?理想情况下,我希望能够以任何顺序调用带有参数的脚本并让它做正确的事情。
parameters powershell scripting parameter-passing optional-parameters
我创建了一个生成 Excel 文件的端点。它应该用作 GET,以防我想要一些其他代码将其发布到不同的端点以发送电子邮件,或者我只想通过在浏览器中手动点击端点来下载 Excel 文件。它正在下载 Excel 文件,但是当我尝试打开它时,我看到消息“Excel 无法打开文件 'blahblah',因为文件格式或文件扩展名无效。请验证文件是否已损坏且文件扩展名是否正确与文件格式匹配。”
收到该错误后,我尝试更改响应内容字段和/或文件扩展名中的 MIME 类型,错误消失,文件打开并显示以下警告:“文件格式和扩展名”等等等等不匹配。文件可能已损坏或不安全。除非您相信其来源,否则请勿打开它。您仍要打开它吗?” 如果我无论如何打开它,文件仍然是空的。
这是我使用我创建的 ExcelPackage 并将其添加到响应中的代码。
var response = HttpContext.Current.Response;
response.ContentType = "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet";
var fileName = string.Format("blahblah-{0}.xls", InstantPattern.CreateWithInvariantCulture("yyyy-dd-M-HH-mm-ss").Format(_clock.Now));
response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
response.BinaryWrite(excelPackage.GetAsByteArray());
Run Code Online (Sandbox Code Playgroud)
我尝试添加不同的 mime 类型,如 application/excel。我试过使用 .xlsx 文件扩展名而不是 xls。什么都没有真正奏效。我知道 ExcelPackage 工作簿的工作表实际上包含我想要的数据,因为当我调试并将鼠标悬停在对象上时,我会看到我希望将其写入文件的单元格值。那么我做错了什么?
我已经尝试以两种方式生成 excelPackage,这两种方式都在 using 块内。像这样:
using (var excelPackage = new ExcelPackage())
{
// generate and download excel file
}
Run Code Online (Sandbox Code Playgroud)
也像这样:
using (var excelPackage = new ExcelPackage(new FileInfo(fileName)))
{
// generate and download excel file
}
Run Code Online (Sandbox Code Playgroud) 允许用户使用字符串数组.他们可以向数组添加字符串,从数组中删除字符串,搜索数组中的字符串,最终他们将能够对数组进行排序.分类是搞砸我的原因.我尝试过几种不同的方法.第一种方法是将数组转换为ArrayList,并使用Collections对ArrayList进行排序,ArrayList将转换回静态类数组.它不起作用.我尝试的第二种方法是迭代数组并尝试仅排序用户添加的字符串而不是数组中的所有内容(因为数组中有一些空值).也许我应该遍历数组,然后将非null值存储到一个新的数组中,然后我可以对它进行排序?但是如果我想在排序新数组后添加更多字符串呢?这就是为什么我停止了第二个解决方案.第三次尝试是在我的数组上使用Arrays.sort()但由于某种原因它不起作用.
这是一个例外:
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at testingSearch.sortArray(testingSearch.java:93)
at testingSearch.main(testingSearch.java:42)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class testingSearch {
static String[] strArray;
static {
strArray = new String[5];
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
System.out.println("1. Add string to the string array.");
System.out.println("2. Remove string from the string array.");
System.out.println("3. Display strings in string array.");
System.out.println("4. Search …Run Code Online (Sandbox Code Playgroud) arrays ×1
c# ×1
collections ×1
epplus ×1
file ×1
get ×1
java ×1
mime ×1
parameters ×1
powershell ×1
scripting ×1
sorting ×1
string ×1