有没有办法在Powershell中从此txt文件中获取数据?

时间:2020-03-23 19:27:15

标签: powershell

我正在使用Beyond Compare,如果txt文件中有任何差异,我已经将其输出。但是,我想在Powershell中编写脚本,以便如果文件中没有差异,脚本将继续执行其他操作。但是,我不确定这是否可能。我已经遍历了Select-String,但是不确定是否能够完成我所寻找的。附件是无差异时txt文件的外观以及无差异时的外观。

Differences No Differences

是否可以将txt文件中的名称,大小或修改后的内容转换为变量,然后对其是否为空做条件?还是有另一种方法可以实现我在Powershell中想要实现的目标?提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您只需知道是否有任何差异,则应执行以下操作:

/**
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
const {
  RichResponse,
  PLATFORMS,
  SUPPORTED_RICH_MESSAGE_PLATFORMS,
  V2_TO_V1_PLATFORM_NAME,
} = require('./rich-response');

/**
 * Enum for Dialogflow v1 text message object https://dialogflow.com/docs/reference/agent/message-objects
 */
const v1MessageObjectCard = 1;

/**
 * Class representing a card response
 * @extends RichResponse
 */
class Card extends RichResponse {
  /**
   * Constructor for Card object.
   *
   * @example
   *
   * @param {string|Object} card response title string or an object representing a card response
   */
  constructor(card) {
    super();
    if (card === undefined || (typeof card === 'object' && !card.title)) {
      throw new Error('title string required by Card constructor');
    }
    if (typeof card === 'string') {
      this.title = card;
    } else if (typeof card === 'object') {
      this.title = card.title;
      this.text = card.text;
      this.imageUrl = card.imageUrl;
      if(card.buttonUrl) {
        this.buttonUrl = card.buttonUrl;
      }
      if(!card.buttonUrl) {
        card.buttonUrl = "buffor"
      }
      if (
        (!card.buttonText && card.buttonUrl) ||
        (!card.buttonUrl && card.buttonText)
      ) {
        throw new Error('card button requires title or title with link');
      }
      this.buttonText = card.buttonText;
      if (
        typeof card.platform !== 'undefined' &&
        card.platform !== PLATFORMS.UNSPECIFIED
      ) {
        if (SUPPORTED_RICH_MESSAGE_PLATFORMS.indexOf(card.platform) < 0) {
          throw new Error(`Platform '${card.platform}' not supported.`);
        } else {
          this.platform = card.platform;
        }
      }
    }
  }

  /**
   * Set the title for a Card
   *
   * @example
   * let card = new Card();
   * card.setTitle('sample card title')
   *
   * @param {string} title containing the title content
   * @return {Card}
   */
  setTitle(title) {
    if (typeof title !== 'string') {
      throw new Error('setText requires a string of the text');
    }
    this.title = title;
    return this;
  }

  /**
   * Set the text for a Card
   *
   * @example
   * let card = new Card();
   * card.setText('sample card body text')
   *
   * @param {string} text containing the card body text content
   * @return {Card}
   */
  setText(text) {
    if (typeof text !== 'string') {
      throw new Error('setText requires a string of the text');
    }
    this.text = text;
    return this;
  }

  /**
   * Set the image for a Card
   *
   * @example
   *
   * @param {string} imageUrl
   * @return {Card}
   */
  setImage(imageUrl) {
    if (typeof imageUrl !== 'string') {
      throw new Error('setImage requires a string of the image URL');
    }
    this.imageUrl = imageUrl;
    return this;
  }

  /**
   * Set the button for a Card
   *
   * @example
   * let card = new Card();
   * card.setButton({
   *     text: 'button text',
   *     url: 'https://assistant.google.com/'
   * });
   *
   * @param {Object} button JSON configuration
   * @param {Object} options.text button text
   * @param {Object} options.url button link URL
   * @return {Card}
   */
  setButton(button) {
    if ((!button.text)) {
      throw new Error(
        `card button requires button title and url. \
\nUsage: setButton({text: \'button text\', url: \'http://yoururlhere.com\'}`
      );
    }
    this.buttonText = button.text;
    if(button.url) {
      this.buttonUrl = button.url;
    }
    return this;
  }

  /**
   * Get the v1 response object for the rich response
   * https://dialogflow.com/docs/reference/agent/message-objects
   *
   * @example
   * let richResponse = new RichResponse();
   * richResponse.getV1ResponseObject_(PLATFORMS.ACTIONS_ON_GOOGLE)
   *
   * @param {string} platform desired message object platform
   * @return {Object} v1 response object
   * @private
   */
  getV1ResponseObject_(platform) {
    // Check if response is platform specific
    if (this.platform && this.platform !== platform) {
      // if it is and is not for the specific platform return null
      return null;
    }

    //
    let response;
    if (platform === PLATFORMS.ACTIONS_ON_GOOGLE) {
      // If the platform is Actions on Google use a simple response
      response = {
        type: 'basic_card',
        platform: V2_TO_V1_PLATFORM_NAME[platform],
      };
      response.title = this.title;
      if (!this.text && !this.imageUrl) {
        response.formattedText = ' '; // AoG requires text or image in card
      }
      if (this.text) response.formattedText = this.text;
      if (this.imageUrl) {
        response.image = {};
        response.image.url = this.imageUrl;
        response.image.accessibilityText = 'accessibility text';
      }

      if (this.buttonText) {
        response.buttons = [{openUrlAction: {}}];
        response.buttons[0].title = this.buttonText;
        if(this.buttonUrl) {
          response.buttons[0].openUrlAction.url = this.buttonUrl;
        }
      }
    } else {
      response = {type: v1MessageObjectCard};
      response.title = this.title;
      if (this.text) response.subtitle = this.text;
      if (this.imageUrl) response.imageUrl = this.imageUrl;
      // this is required in the response even if there are no buttons for some reason
      if (platform === PLATFORMS.SLACK) response.buttons = [];
      if (this.buttonText && this.buttonUrl) {
        response.buttons = [];
        response.buttons[0] = {};
        response.buttons[0].text = this.buttonText;
        if(this.buttonUrl) {
          response.buttons[0].postback = this.buttonUrl;
        }
      }
      // response is the same for generic responses without the platform attribute
      // if the platform is not undefined or the platform is not unspecified
      if (SUPPORTED_RICH_MESSAGE_PLATFORMS.indexOf(platform) > -1) {
        response.platform = V2_TO_V1_PLATFORM_NAME[platform];
      }
    }
    return response;
  }

  /**
   * Get the v2 response object for the rich response
   * https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/projects.agent.intents#Message
   *
   * @example
   * let richResponse = new RichResponse();
   * richResponse.getV2ResponseObject_(PLATFORMS.ACTIONS_ON_GOOGLE)
   *
   * @param {string} platform desired message object platform
   * @return {Object} v2 response object
   * @private
   */
  getV2ResponseObject_(platform) {
    // Check if response is platform specific
    if (this.platform && this.platform !== platform) {
      // if it is and is not for the specific platform return null
      return null;
    }

    let response;
    if (platform === PLATFORMS.ACTIONS_ON_GOOGLE) {
      // If the platform is Actions on Google use a basic card response
      response = {basicCard: {}};
      response.basicCard.title = this.title;
      response.platform = PLATFORMS.ACTIONS_ON_GOOGLE;
      if (this.text) response.basicCard.formattedText = this.text;
      if (this.imageUrl) {
        response.basicCard.image = {};
        response.basicCard.image.imageUri = this.imageUrl;
        response.basicCard.image.accessibilityText = 'accessibility text';
      }
      if (this.buttonText && !this.buttonUrl) {
        response.basicCard.buttons = [{
          title: this.buttonText
        }];
      }
      if (this.buttonText && this.buttonUrl) {
        response.basicCard.buttons = [{
          title: this.buttonText,
          openUriAction: {
            uri: this.buttonUrl,
          },
        }];
      }
    } else {
      response = {card: {}};
      response.card.title = this.title;
      if (this.text) response.card.subtitle = this.text;
      if (this.imageUrl) response.card.imageUri = this.imageUrl;
      if (this.buttonText && !this.buttonUrl) {
        response.card.buttons = [];
        response.card.buttons[0] = {};
        response.card.buttons[0].text = this.buttonText;
      }
      if (this.buttonText && this.buttonUrl) {
        response.card.buttons = [];
        response.card.buttons[0] = {};
        response.card.buttons[0].text = this.buttonText;
        response.card.buttons[0].postback = this.buttonUrl;
      }
      // response is the same for generic responses without the platform attribute
      // if the platform is not undefined or the platform is not unspecified
      if (SUPPORTED_RICH_MESSAGE_PLATFORMS.indexOf(platform) > -1) {
        response.platform = platform;
      }
    }
    return response;
  }
}

module.exports = Card;
  • $noDiffs = '' -eq ((Get-Content -Raw report.txt) -split '\r?\n-+\r?\n')[1].Trim() 使用regex(正则表达式)按照表头行之后的分隔线((Get-Content -Raw report.txt) -split '\r?\n-+\r?\n)分割整个输入文件。

  • ----...看起来是结果数组的第二个元素,即,分隔线之后的所有内容,都会修剪所有前导和尾随空格,并测试结果是否为空字符串。

答案 1 :(得分:0)

对于@ mklement0的问题和答案,都表示尊重。
在PowerShell脚本中使用Beyond Compare会将cart before the horse放入。
PowerShell中有许多cmdlet,可以让您轻松比较文件夹(以及更多),而无需进行任何文本抓取。 对于给定的示例,将Compare-ObjectGet-ChildItem一起使用:

Compare-Object (Get-ChildItem .\Test) (Get-ChildItem .\Test1) -Property Name, Length, LastWriteTime

如果要在相对路径上进行递归比较,可以执行以下操作:

Compare-Object (Get-ChildItem .\Test -Recurse -Name) (Get-ChildItem .\Test1 -Recurse -Name)

请注意,-Name参数只会列出相对路径字符串,如果您还想比较LengthLastWriteTime,则可以执行以下操作:

$TestFolder = 'C:\Test'
$Test1Folder = 'C:\Test1'

$TestFiles  = Get-ChildItem $TestFolder  -File -Recurse |
    Select-Object *,@{N='RelativePath'; E={$_.FullName.SubString($TestFolder.Length)}}
$Test1Files = Get-ChildItem $Test1Folder -File -Recurse |
    Select-Object *,@{N='RelativePath'; E={$_.FullName.SubString($Test1Folder.Length)}}

Compare-Object $TestFiles $Test1Files -Property RelativePath,Length,LastWriteTime