如何在Powershell中跨多个范围查询主机名的Windows DHCP服务器

时间:2017-01-11 21:54:38

标签: powershell dhcp

我正在尝试编写一个允许我指定主机名的内核,然后搜索该DHCP租约的scopeID列表。 Powershell版本:

SELECT
    CY.CycleGroupName AS CycleName,
    FF.WBSCode, 
    CC.CostCenterCode,
    CC.Name AS CostCenterName,
    IIF(FF.ClientSpecific = 'Overhead', CC.GlobalLocal, 'Local') AS GlobalLocal, 
    PC.ProfitCenterCode,
    PC.Name AS ProfitCenterName,
    FN.ForecastNodeCode AS ForecastNodeCode,
    FN.Name AS ForecastNodeName,
    FN.GSGeographicUnit,
    FN.GSCountry,
    FN.Domain,
    FN.Location,
    CO.Name AS CountryName,
    CO.Classification AS CountryClassification,
    GU.Name AS GeographicUnitName,
    GU.GeographicAreaCode,
    ReportLine2,
    ReportLine3,
    ReportLine4,
    IIF(RL.IsGrossCost = 'Yes', 'Gross Cost', '') AS GrossCostClassification,
    ProductivityCategory,
    FF.ClientSpecific,
    FF.ResourceID,
    FF.CurrencyType,
    CO.CurrencyCode AS LocalCurrencyCode, 
    FF.InputCurrencyCode AS InputCurrencyCode, 
    FF.Comments,
    FF.FiscalMonthNumber,
    FF.Amount * Multiplier AS Amount
FROM
(
    SELECT
        CQ.CycleName,
        FF.TemplateID,
        FF.HeadcountID,
        FF.BorrowedID,
        CT.CurrencyType, 
        FF.WBSCode,
        FF.CostCenterCode,
        FF.ProfitCenterCode,
        FF.ForecastNodeCode,
        FF.CountryCode,
        FF.ReportLine,
        FF.CostElementCode,
        'T' + CAST(FF.TemplateID AS NVARCHAR) + 'H' + IIF(FF.HeadcountID IS NOT NULL, 'H' + CAST(FF.HeadcountID AS NVARCHAR), 'B' + CAST(FF.BorrowedID AS NVARCHAR)) AS ResourceID,
        FF.InputCurrencyCode,
        FF.ProductivityCategory,
        FF.ClientSpecific,
        FF.Comments,
        FF.FiscalMonthNumber,
        IIF(CT.Number = 30, FF.Amount * FX.Sep, FF.Amount) AS Amount
    FROM dbo.FinancialsForecast FF
        LEFT JOIN dbo.CyclesQuarters CQ ON FF.QuarterCode = CQ.QuarterCode
        LEFT JOIN dbo.CurrencyTypes CT ON 1 = 1
        LEFT JOIN dbo.CostCenters CC ON FF.CostCenterCode = CC.CostCenterCode 
        LEFT JOIN dbo.Countries CO ON ISNULL(CC.CountryCode, FF.CountryCode) = CO.CountryCode
        LEFT JOIN dbo.FXRates FX ON CO.CurrencyCode = FX.CurrencyCode AND FF.Year = FX.Year AND FF.QuarterCode = FX.QuarterCode
    WHERE
        CQ.CycleName IS NOT NULL
        AND FiscalMonthNumber > CQ.LastActualsMonth
    UNION ALL
    SELECT
        ISNULL(CQ.CycleName, 'Previous Actuals') AS CycleName,
        NULL AS TemplateID,
        NULL AS HeadcountID,
        NULL AS BorrowedID,
        FA.CurrencyType, 
        FA.WBSCode,
        FA.CostCenterCode,
        FA.ProfitCenterCode,
        NULL AS ForecastNodeCode,
        FA.CountryCode,
        NULL AS ReportLine,
        FA.CostElementCode,
        NULL AS ResourceID,
        NULL AS InputCurrencyCode,
        ProductivityCategory,
        FA.ClientSpecific,
        NULL AS Comments,
        FA.FiscalMonthNumber,
        FA.Amount
    FROM dbo.FinancialsActuals FA
        LEFT JOIN dbo.CyclesQuarters CQ ON FA.Year = (SELECT Value FROM dbo.Settings WHERE Name = 'CurrentYear')
        LEFT JOIN dbo.WBS W ON FA.WBSCode = W.WBSCode
        LEFT JOIN dbo.BusinessActivitiesLevel1 B1 ON W.BusinessActivityLevel1Code = B1.BusinessActivityLevelCode
    WHERE
        FA.Year >= (SELECT CAST(Value AS INT) FROM dbo.Settings WHERE Name = 'CurrentYear') - 1
        AND FiscalMonthNumber <= ISNULL(CQ.LastActualsMonth, 12)
) FF
    LEFT JOIN dbo.CostCenters CC ON FF.CostCenterCode = CC.CostCenterCode
    LEFT JOIN dbo.Countries CO ON ISNULL(CC.CountryCode, FF.CountryCode) = CO.CountryCode
    LEFT JOIN dbo.ProfitCenters PC ON CC.ProfitCenterCode = PC.ProfitCenterCode 
    LEFT JOIN dbo.ForecastNodes FN ON ISNULL(ISNULL(CC.ForecastNodeCode, FF.ForecastNodeCode), PC.ForecastNodeCode) = FN.ForecastNodeCode 
    LEFT JOIN dbo.GeographicUnits AS GU ON CO.GeographicUnitCode = GU.GeographicUnitCode 
    LEFT JOIN dbo.CostElements CE ON FF.CostElementCode = CE.CostElementCode
    LEFT JOIN dbo.ReportLines RL ON ISNULL(CE.ReportLine, FF.ReportLine) = RL.ReportLine AND RL.Number = 4
    LEFT JOIN dbo.ReportLinesLevels RLL ON RL.ReportLine = RLL.ReportLine4
    LEFT JOIN dbo.CyclesGroups CY ON FF.CycleName = CY.CycleName

到目前为止,我正在使用它:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  576

这有效,但我必须使用find / replace更改每行的主机名。

我在考虑类似的东西,

Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.16.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.17.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.18.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.19.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.76.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate 

但阵列在ScopeId下不起作用。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用foreach或别名%来管道IP地址:

"172.17.16.0","172.17.17.0","172.17.18.0","172.17.19.0","172.17.76.0" | % {
    Get-DhcpServerv4Lease -ComputerName Server -ScopeId $_ |
    Where-Object HostName -match Hostname |
    FT ipaddress,hostname,clientid,addressstate
}

答案 1 :(得分:0)

以下不是一个单行,但对于那些手动识别范围比实际范围更多的人来说:

$allscope = Get-DhcpServerv4Scope -ComputerName "dhcpserver"

foreach ($ScopeID in $allscope) 
{Get-DhcpServerv4Lease -ComputerName "dhcpserver" -ScopeId $ScopeID.scopeid | 
where {$_.hostname -match "hostname"}}