小编Mar*_*son的帖子

为什么我不能删除Perl中的这个空目录?

我正在从http://www.perlmonks.org/index.pl?node_id=217166转换linux脚本,具体如下:

#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use File::Find;

@ARGV > 0 and getopts('a:', \my %opt) or die << "USAGE";
# Deletes any old files from the directory tree(s) given and
# removes empty directories en passant.
usage: $0 [-a maxage] directory [directory ...]
       -a  maximum age in days, default is 120
USAGE

my $max_age_days = $opt{a} || 120;

find({
    wanted => sub { unlink if -f $_ and -M _ > $max_age_days },
    postprocess => sub { …
Run Code Online (Sandbox Code Playgroud)

perl directory-traversal find

4
推荐指数
2
解决办法
2246
查看次数

如何将方法名称作为参数传递?

我刚刚注意到我在ASP.NET应用程序中重复了很多C#代码,所以想要创建一个通用方法.我有一系列这样的私有方法:

private void PopulateMyRepeatedControl()
{
    DBUtil DB = new DBUtil();
    DataTable symbols = GetSelectedSymbols();
    DataTable tradeGrades = GetSelectedTradeGrades();
    DataTable executionGrades = GetSelectedExecutionGrades();        

    chtMyRepeatedChart.DataSource = DB.MyRepeatedCall (
        int.Parse(txtStartBalance.Text),
        int.Parse(ddlTradeTypes.SelectedValue),
        ddlRepeatedTrades.SelectedValue,
        radSide.SelectedValue,
        ddlTradeSetups.SelectedValue,
        symbols,
        ddlChartTimeFrames.SelectedValue,
        int.Parse(ddlHours.SelectedValue),
        int.Parse(ddlYears.SelectedValue),
        int.Parse(ddlMonths.SelectedValue),
        int.Parse(ddlDays.SelectedValue),
        int.Parse(ddlNumSCs.SelectedValue),
        txtDateFrom.Text,
        txtDateTo.Text,
        tradeGrades,
        executionGrades,
        int.Parse(txtMinProfitPips.Text),
        int.Parse(txtMaxProfitPips.Text));

    chtMyRepeatedChart.DataBind();
}
Run Code Online (Sandbox Code Playgroud)

所以,我想替换DB.MyRepeatedCall,chtMyRepeatedChart并将它们作为参数传递给泛型函数.那可能吗?我的表单上有很多图表,它们使用相同数量的参数.

谢谢

更新 根据Frederik的解决方案,我做到了这一点:

private delegate IEnumerable<DataTable> GetDataSource(
    int TradeType,
    string RepeatedTrades,
    string Side,
    string TradeSetup,
    DataTable symbols,
    string ChartTimeFrame,
    int Hour,
    int Year,
    int Month,
    int Day,
    int NumSCs, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net c#-4.0

4
推荐指数
1
解决办法
1828
查看次数

如何在VBScript中将字符串转换为double?

我需要在VBScript中编写一些代码,并在文本文件中有一个版本号字符串,我需要与之进行比较.如果我将此代码编写为测试:

option explicit

Dim VersionString
VersionString = "6.2.1"

Dim Version
Version = CDbl (VersionString)

Version = Version * 100
Run Code Online (Sandbox Code Playgroud)

我在CDbl上遇到错误:

Microsoft VBScript runtime error: Type mismatch: 'CDbl'

我该如何阅读和比较这个字符串值?

vbscript

4
推荐指数
1
解决办法
3万
查看次数

Powershell数组范围; 为什么我的数组是空的?

我想有一个数组,并从我的脚本中的不同函数添加元素.我下面的例子说明了我可能误解了有关范围的问题.我当前的理解是,如果在函数外部定义了一个数组,然后在函数内部添加了元素,那么这些元素应该在函数外部可用.

Function ListRunningServices
{
    $services+= Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name;
}

$services = @();
ListRunningServices;
$services;
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?也许我的风格完全错了.

arrays powershell powershell-2.0

4
推荐指数
2
解决办法
8626
查看次数

Powershell 模块必须放置在单独的文件夹中吗?

使用PowerShell 2.0

根据我在网上读到的内容,用户创建的 powershell 模块必须各自驻留在自己的目录中。例如,如果我创建一个名为的模块,MyModule.psm1它必须驻留在名为 的文件夹中MyModule,并驻留在$env:PSModulePath.

如果我的项目有很多模块,那么为每个模块创建一个单独的文件夹对我来说似乎很愚蠢。这真的有必要吗?为什么?有什么优雅的方法吗?

powershell powershell-2.0

4
推荐指数
1
解决办法
734
查看次数

如何搜索对象数组包含另一个数组中的项?

我有两个数组.

包含虚拟机信息的对象数组,称为$vms其中一个属性Name.这是类型:

PowerCLI > $vms.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
Run Code Online (Sandbox Code Playgroud)

我有一个从CSV文件导入的另一个数组,$importVMs其中一个字段也被调用Name.

如果$importVMs.Name不存在,我想做一些工作$vms(即它不匹配任何$vms.Name).我想知道我是否可以使用流水线操作,或者我是否必须遍历两个数组?

我能做点什么吗 if (! $vms | ? {$_.Name -neq $importVms.Name) { # work }

似乎无法让它发挥作用.我需要foreach$importVmsif条件?

编辑

到目前为止我的完整脚本:

Connect-VIServer -Server vCenter -Protocol https -Force | out-null
$importVms = Import-Csv vCenterVMs.csv
$VMHost = "esxi"
$currentVms = Get-VM
Write-Host "Current registered VMs" -ForeGroundColor Cyan
$currentVMs …
Run Code Online (Sandbox Code Playgroud)

powershell vmware powershell-2.0 powercli

3
推荐指数
1
解决办法
1万
查看次数

如何在PowerShell中使用带参数集的默认值?

我有这个功能

Function Do-ThisOrThat
{
[cmdletbinding()]
param (
    [Parameter(Mandatory = $false, ParameterSetName="First")]
    [string]$FirstParam = "MyFirstParam",

    [Parameter(Mandatory = $false, ParameterSetName="Second")]
    [string]$SecondParam
    )
    Write-Output "Firstparam: $FirstParam. SecondParam $SecondParam"
}
Run Code Online (Sandbox Code Playgroud)

如果我像这样调用函数,Do-ThisOrThat我希望它检测到$ FirstParam有一个默认值并使用它.可能吗?只有在我指定参数时才能运行它.

例如,这工作: Do-ThisOrThat -FirstParam "Hello"

powershell powershell-4.0

3
推荐指数
1
解决办法
2303
查看次数

如何使用SSH和PowerShell中的私钥连接到Linux服务器?

我已经使用puttygen生成了一个ssh密钥对,并且可以使用Windows 10工作站上的putty的私钥成功连接到CentOS服务器:

成功连接到CentOS的图像

我想从Windows PowerShell连接到服务器,并使用以下命令加载了Posh-SSH模块: Install-Module posh-ssh

我尝试使用以下命令创建一个新的ssh会话:

$Credential = Get-Credential
$KeyFile = 'C:\Users\mark\Documents\ssh\privkey.ppk'
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -Keyfile $KeyFile
Run Code Online (Sandbox Code Playgroud)

我输入了root和空白密码,Get-Credential但出现此错误:

New-SSHSession:无效的私钥文件。

我试图通过读取私钥文件并将其转换为以base64编码的方式将privkey文件转换为字符串,但出现相同的错误:

$privkeyString = Get-Content $KeyFile
$Bytes = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($privkeyString))
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $Bytes
Run Code Online (Sandbox Code Playgroud)

我也试过了,但是得到了同样的错误:

$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $privkeystring
Run Code Online (Sandbox Code Playgroud)

关于如何使用带有私钥文件的PowerShell连接到linux服务器的任何想法?

ssh powershell powershell-5.0

3
推荐指数
1
解决办法
3076
查看次数

带有条件的 PowerShell switch 语句

如何在 PowerShellswitch语句中添加条件?下面的代码在0应该返回时返回1000

$UsedSpaceMB = 8500

switch ($UsedSpaceMB) {
    ($UsedSpaceMB -gt 15000) { $FreeSpace = 2000 }
    ($UsedSpaceMB -in 8000..15000 ) { $FreeSpace = 1000 }
    ($UsedSpaceMB -in 1700..8000 ) { $FreeSpace = 500 }
    ($UsedSpaceMB -in 1000..1700 ) { $FreeSpace = 200 }
    ($UsedSpaceMB -in 800..1000 ) { $FreeSpace = 100 }
    ($UsedSpaceMB -lt 800 ) { $FreeSpace = 50 }
}
$FreeSpace
Run Code Online (Sandbox Code Playgroud)

powershell conditional-statements

3
推荐指数
1
解决办法
3400
查看次数

如何在 Azure DevOps 中设置 Azure 应用服务的 ConnectionString 配置?

我正在部署一个用 Asp.Net Core 2.2 编写的网站,该网站连接到 Azure Sql 数据库。在Azure DevOps中的Azure 应用服务部署任务中,在哪里设置连接字符串?

在我的 WebApp 中,它DefaultConnectionappsettings.json本地运行良好。我想在部署到 Azure 时覆盖此设置。

我尝试-DefaultConnection $(DefaultConnection)Azure 应用服务部署任务的“应用程序和配置设置”部分中进行设置,但未设置连接字符串。该值位于 Azure DevOps 中的变量中。

我哪里错了?

azure azure-web-app-service azure-devops

3
推荐指数
1
解决办法
6616
查看次数