从文本文件

时间:2016-06-14 14:33:22

标签: csv powershell powershell-v2.0

我的脚本输出托管的任何网站的文本文件。文本文件如下所示:

Default
Settings
Layouts
Stackexchangelogon

我需要将这些文件放入包含以下标题的CSV文件中:

  • 类型
  • 状态

对于Type,我需要网站名称,Base,我需要它有IIS和Status,我需要监控。

最终输出将是,

Type                  Base          Status
Default               IIS           Monitored
Settings              IIS           Monitored
Layouts               IIS           Monitored
Stackexchangelogon    IIS           Monitored

我在网上搜索过,仍然对如何实现这一目标感到困惑。由于Windows 2003主机,它需要在PowerShell 2.0上运行。

这是我到目前为止 -

$website = Get-content "D:\Websites.txt"

$CSV = @()
$row = New-Object System.Object
$website | ForEach-Object {
  $row | Add-Member -MemberType NoteProperty -Force -Name "Type" -Value $_
  $row | Add-Member -MemberType NoteProperty -Force -Name "Base" -Value "IIS"
  $row | Add-Member -MemberType NoteProperty -Force -Name "Status" -Value "Monitored"

  $CSV += $row
}

但它没有添加每个网站。我重复列表中的最后一个。结果:

Type                         Base                         Status
----                         ----                         ------
Stackexchangelogon           IIS                          Monitored
Stackexchangelogon           IIS                          Monitored
Stackexchangelogon           IIS                          Monitored
Stackexchangelogon           IIS                          Monitored

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您问题中的代码不起作用,因为您在循环外创建<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=301880 --> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="FamilyAppConnectionString" connectionString="Data Source=JN-LAPTOP\SQLEXPRESS;Initial Catalog=FamilyApp;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <httpModules> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> </httpModules> <webServices> <protocols> </protocols> </webServices> </system.web> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> </assemblyBinding> </runtime> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> </compilers> </system.codedom> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules> <remove name="ApplicationInsightsWebTracking" /> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> </modules> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers></system.webServer> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <behaviors> <endpointBehaviors> <behavior name="WebApplication6.EndPointBehavior"> <webHttp defaultBodyStyle="WrappedRequest" defaultOutgoingResponseFormat="Json" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <webHttpBinding> <binding name="HttpBinding" transferMode="Streamed"> </binding> </webHttpBinding> </bindings> <services> <service name="WebApplication6.Service1"> <host> <baseAddresses> <add baseAddress="http://localhost:61512/" /> </baseAddresses> </host> <endpoint behaviorConfiguration="WebApplication6.EndPointBehavior" binding="webHttpBinding" contract="WebApplication6.IService" bindingConfiguration="HttpBinding" /> <endpoint address="/CreatePerson " binding="wsHttpBinding" behaviorConfiguration="WebApplication6.EndPointBehavior" contract="WebApplication6.IService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel> </configuration> 并只修改循环内的这一个对象。 $row然后将引用附加到数组的对象。因为你的数组充满了对同一个对象的引用,输出显示循环后这个单个对象的最终状态。

有几种方法可以避免这种情况,例如通过每次迭代创建一个新对象(就像在your own answer中一样),或者通过将对象的 clone 附加到阵列:

$CSV += $row

但是,这两种方法都不是最理想的,因为它们没有正确使用管道。使用calculated properties

可以简化您的代码
$CSV += $row.PSObject.Copy()

答案 1 :(得分:0)

使用 -

解决
task