Invoke-Command我在脚本块采用字典类型参数的地方运行时遇到错误:
无法处理参数“字典”的参数转换。无法将“System.Collections.Hashtable”类型的“System.Collections.Hashtable”值转换为“System.Collections.Generic.IDictionary`2[System.String,System.String]”类型。在行:7 字符:1 + Invoke-Command -ComputerName 。-ArgumentList $dictionary -ScriptBlock ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [], ParameterBindin...mationException + FullQualifiedErrorId : ParameterArgumentTransformationError + PS计算机名称:本地主机
经过大量挖掘后,我能够将脚本简化为下面的 MVP,以显示此问题的根源:
[System.Collections.Generic.IDictionary[string, string]]$dictionary = New-Object -TypeName 'System.Collections.Generic.Dictionary[string, string]'
$dictionary.Add('one', 'hello')
$dictionary.Add('two', 'world')
Write-Verbose "Main Script $($dictionary.GetType().FullName)" -Verbose #outputs: VERBOSE: Before System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Invoke-Command -ComputerName . -ArgumentList $dictionary -ScriptBlock {
Param (
#[System.Collections.Generic.IDictionary[string, string]] #if I leave this in I get a conversion error
$dictionary
) …Run Code Online (Sandbox Code Playgroud) 我被要求将使用的bash脚本转换nc -z为PowerShell。
该站点告诉我nc -z测试端口是否“未连接”打开。有什么方法可以使用.Net做到这一点?
通常,要测试端口是否打开,我会用.Net TcpClient创建一个连接。如果连接成功,我也会报告所有问题。MVE:
$isConnected = $false
$client = New-Object -TypeName 'System.Net.Sockets.TcpClient'
try {
$client.Connect($host, $port)
$isConnected = $client.Connected
$client.GetStream().Close(); # handle bug in .net 1.1
$client.Close();
} catch {
$isConnected = $false
} finally {
if ($client.Dispose) {$client.Dispose()} # dispose method not available in all versions, so check before calling
}
Run Code Online (Sandbox Code Playgroud)
但是,尽管不发送任何数据,但它确实可以连接。
可能是由于的描述具有nc -z误导性/只是意味着没有发送数据...我无法以任何方式找到确认。
我正在尝试在容器映像上启用远程桌面。
FROM mcr.microsoft.com/windows:2004
EXPOSE 3389
RUN net user administrator Stack0verflow
RUN net user administrator /active:yes
# I tried disabling the firewall; but this command errors as Windows Defender Firewall service
# is not enabled; so presumably if the firewall's not running, it's not a firewall issue.
#RUN netsh advfirewall set allprofiles state off
# switch shell to powershell (note: pwsh not available on the image)
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; $ExecutionPolicy = 'Unrestricted';"]
# enable …Run Code Online (Sandbox Code Playgroud) containers remote-desktop terminal-services docker windows-10
我想知道是否有人找到解决这个问题的巧妙方法。我试图从几个表中选择数据,让记录逐行匹配。我基本上追求的是完整的外部连接,但有一个关键的区别。如果我在一个表中连接的列中有四行具有特定值,而另一个表中有三行具有该值,我只希望连接前三个结果,第四个结果就像有一直不匹配。
这样做的原因是创建一个对账报告,以确保在比较结果时不会多次计算交易。我可以通过使用一些分组和一些聚合函数来解决这个问题,但这隐藏了我想保留的一些细节。
下面是一个示例,展示了我所追求的东西,注释中的无效/伪代码说明了我如何认为这是有效的:
declare @t1 table (id bigint identity(1,1) primary key clustered, foreignKeyId bigint, otherData nvarchar(10))
declare @t2 table (id bigint identity(1,1) primary key clustered, foreignKeyId bigint, moreData nvarchar(10))
insert @t1 select 1, '1.1.1'
union all select 1, '1.1.2'
union all select 1, '1.1.3'
union all select 3, '1.3.1'
union all select 3, '1.3.2'
union all select 3, '1.3.3'
union all select 4, '1.4.3'
insert @t2 select 1, '2.1.1'
union all select 1, '2.1.2'
union all select 1, '2.1.3'
union …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
var countries = from c in db.Countries
where (string.IsNullOrWhiteSpace(searchAlpha2) || (c.Alpha2 ?? string.Empty).ToUpper().Contains(searchAlpha2.ToUpper()))
&& (string.IsNullOrWhiteSpace(searchAlpha2) || (c.Alpha3 ?? string.Empty).ToUpper().Contains(searchAlpha3.ToUpper()))
&& (string.IsNullOrWhiteSpace(searchName) || (c.Name ?? string.Empty).ToUpper().Contains(searchName.ToUpper()))
select c;
Run Code Online (Sandbox Code Playgroud)
此代码在SQL数据库上使用Entity Framework v6 Code First.
除了性能之外,如果我不包括IsNullOrWhitespace我在过滤条件为空时没有得到结果(我已经测试了空值和空值); 但是当值存在时,这可以按预期工作.
我收到错误:
LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression.
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用searchXXX字符串来过滤列.我已经尝试过使用RegEx.IsMatch,SqlMethods.Like和下面的代码,但是所有这些都给我错误,说明这些函数是不允许的(错误来自EntityFramework.SqlServer或来自Linq to Entities).我已经看到很多帖子在这里成功完成了 - 所以我想知道我是否遗漏了一些基本的东西?
c# linq-to-entities linq-to-sql entity-framework-6 asp.net-mvc-5
Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.LambdaExpression'运行以下代码时出现错误。
这段代码的目的是让我过滤包含某些字符串的记录(实体框架代码优先/Linq to SQL)。
注意:我正在使用第三方库 LinqKit:http : //www.albahari.com/nutshell/predicatebuilder.aspx
FilterHelper<Country> helper = new FilterHelper<Country>();
helper.AddContains(searchAlpha2, c => c.Alpha2);
helper.AddContains(searchAlpha3, c => c.Alpha3);
helper.AddContains(searchName, c => c.Name);
IQueryable<Country> countries = db.Countries.AsExpandable().Where(helper.Predicate);
Run Code Online (Sandbox Code Playgroud)
...
public class FilterHelper<T>
{
public delegate string GetColumn<T>(T item);
private Expression<Func<T,bool>> predicate;
public FilterHelper()
{
this.predicate = PredicateBuilder.True<T>();
}
public void AddContains(string searchText, GetColumn<T> getColumn)
{
if (!string.IsNullOrWhiteSpace(searchText))
predicate = predicate.And(c => getColumn(c) != null ? getColumn(c).Contains(searchText) : false); …Run Code Online (Sandbox Code Playgroud) 背景
以下代码返回给定别名或主机的 IPv4 地址:
[System.Net.Dns]::GetHostAddresses('someDnsName').IPAddressToString
以下代码返回 IP 的主机名 (CName) 和别名:
[System.Net.Dns]::GetHostByAddress('172.12.34.56')
因此,我希望任何返回 IP 的内容都GetHostAddresses列在调用的主机名或别名下GetHostByAddress(或者至少列出该项目的 FQDN)。即我希望以下查询的结果返回 true
cls
$name = 'someName'
$fqdn = [System.Net.Dns]::GetHostEntry($name).HostName
$ip = [System.Net.Dns]::GetHostAddresses($fqdn).IPAddressToString
$result = [System.Net.Dns]::GetHostByAddress($ip)
#this is the result I'd expect to be true
($result.HostName -eq $fqdn) -or ($result.Aliases -contains $fqdn)
#here's additional info to aid in sense checking
"Name: $name"
"FQDN: $fqdn"
"IP: $ip"
"Result: "
(" - HostName: {0}" -f $result.HostName)
" - Aliases: "
($result | select -ExpandProperty Aliases) …Run Code Online (Sandbox Code Playgroud) 题
该类DocumentFormat.OpenXml.Drawing.BlipExtension有一个名为的属性Uri.在众多例子中,我看到了28A0092B-C50C-407e-A947-70E740481C1C使用的价值; 但从来没有发现这个值是什么/它是否重要,或者它是否是任意的,只是因为人们从现有的示例代码中复制粘贴而流行.
有谁知道这个GUID值的相关性是什么?
背景
我一直在修复遗留应用程序中的一个错误,并且一直在重构以删除一些代码的spaghettification.
在这样做的同时,我发现了一些硬编码的值; 插入到文档中的任何图像都会给出名称Koala.jpg.该系统与考拉无关; 意味着该人只是简单地复制粘贴了一些代码而没有考虑这些值的上下文/含义.另一个硬编码值是BlipExtension的Uri : 28A0092B-C50C-407e-A947-70E740481C1C. 我想确定这个GUID是否有任何特殊含义,或者它是否只是一个唯一值.谷歌搜索显示了许多示例代码中使用的这个值,但到目前为止,我还没有找到它的解释.以下示例结果.
查看BlipExtension文档并未提供有关如何找出此GUID /此引用的架构的详细信息. https://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.blipextension.uri(v=office.14).aspx
我有一些代码删除一个文件夹,然后将文件从一个临时目录复制到该文件夹所在的位置。
Remove-Item -Path '.\index.html' -Force
Remove-Item -Path '.\generated' -Force -Recurse #folder containing generated files
#Start-Sleep -Seconds 10 #uncommenting this line fixes the issue
#$tempDir contains index.html and a sub folder, "generated", which contains additional files.
#i.e. we're replacing the content we just deleted with new versions.
Get-ChildItem -Path $tempDir | %{
Move-Item -Path $_.FullName -Destination $RelativePath -Force
}
Run Code Online (Sandbox Code Playgroud)
Move-Item : Cannot create a file when that file already exists.在generated路径的“移动项”行上出现间歇性错误。
我已经可以通过Start-Sleep -Seconds 10在第二个Remove-Item声明之后添加一个hacky来防止这种情况;尽管这不是一个很好的解决方案。
我认为问题在于,在操作系统赶上实际文件删除之前,Remove-Item语句完成/代码移至下一行。尽管这看起来很奇怪/令人担忧。注意:生成的文件夹中大约有2500个文件(全部在1-100 …
谁能解释select -first 0下面代码中的示例发生了什么?
Function Test-Example {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
$InputObject
)
process {
$global:x++
write-verbose 'I''m running!'
$InputObject
}
}
[int]$global:x = 0 #reset the counter
1..100 | Test-Example -Verbose | select -first 10
$global:x #outputs 10
$global:x = 0 #reset the counter
1..100 | Test-Example | select -first 1000
$global:x #outputs 100; as we only iterate 100 times depsite asking for the first 1000
$global:x = 0 #reset the counter
1..100 | Test-Example | …Run Code Online (Sandbox Code Playgroud) powershell ×5
.net ×3
c# ×2
linq-to-sql ×2
containers ×1
dictionary ×1
directory ×1
dns ×1
docker ×1
drawing ×1
file ×1
hashtable ×1
join ×1
lambda ×1
linqkit ×1
ms-word ×1
openxml ×1
outer-join ×1
psobject ×1
sql ×1
sql-server ×1
tcpclient ×1
vb.net ×1
windows-10 ×1