• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Powershell小技巧之使用-F方法带入数据

Powershell小技巧之使用-F方法带入数据

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了自动带入数据,excel数据带入公式,excel数据带入,powershell,windows powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

封闭在双引号中的字符串能够直接使用变量,这是常用的手法,如代码:

$name = $host.Name 
"Your host is called $name."
</div>

可是这个技巧也有限制。如果你想要显示对象的属性而不是这个变量的本身,例如这样将会失败:

PS> "Your host is called $host.Name."
Your host is called System.Management.Automation.Internal.Host.InternalHost.Name.
</div>

这是因为PS仅能解决变量的本身(如$host),而不支持它的属性。

同时你也不能控制数字的格式,执行下面代码,结果看起来有很多位数字:

# get available space in bytes for C: drive 
$freeSpace = ([WMI]'Win32_LogicalDisk.DeviceID="C:"').FreeSpace 
 
# convert to MB 
$freeSpaceMB = $freeSpace / 1MB 
 
# output 
"Your C: drive has $freeSpaceMB MB space available."
</div>

这里有一个 -F 方法能同时解决这些问题。只需要将它放在模版文本的左边,它的值就会被正确的带入:

# insert any data into the text template 
'Your host is called {0}.' -f $host.Name 
 
# calculate free space on C: in MB 
$freeSpace = ([WMI]'Win32_LogicalDisk.DeviceID="C:"').FreeSpace 
$freeSpaceMB = $freeSpace /1MB 

# output with just ONE digit after the comma 
'Your C: drive has {0:n1} MB space available.' -f $freeSpaceMB
</div>

现在你看,使用-F让你有两个有利条件:这里带括号的占位符指出了带入参数的起始位置,同时它还接受格式。“n1”代表保留1位小数。可以改变它来满足你的需求。

支持PS所有版本

</div>

您可能想查找下面的文章:

  • Powershell小技巧之使用-F方法带入数据

相关文章

  • PowerShell中使用ArrayList实现数组插入、删除、添加例子
  • PowerShell中使用通配符匹配文件路径的例子
  • Powershell小技巧之获取注册表值的类型
  • Windows Powershell 复制数组
  • Powershell实现编写和运行脚本
  • Powershell中定义常量的方法
  • Python中调用PowerShell、远程执行bat文件实例
  • 25个常用PowerShell命令总结
  • Powershell小技巧之获取字符串的行数
  • PowerShell中调用外部程序和进程操作命令例子

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell单行注释、多行注释、块注释的方法
    • Powershell中使用WMI工具例子
    • PowerShell函数参数指定数据类型实例
    • Powershell后台作业、异步操作实例
    • PowerShell中获取Windows系统序列号的脚本分享
    • PowerShell Contains函数查找字符串实例
    • 了解Powershell中的Exit函数
    • Powershell实现捕获系统内置EXE程序的异常
    • Powershell使用WINDOWS事件日志记录程序日志
    • PowerShell获取系统环境变量的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有