2016-05-13 29 views
0

我正在研究生成Outlook html签名的powershell脚本。 现在的作品,但你需要填写所有手动模板字(名电子邮件地址电话等)Powershell读取csv并获取等于用户名的数据

我想要的是,当你运行PowerShell脚本会检查登录用户,并获取信息的用户从CSV文件中并将其填充到html模板中。

这样做最好的方法是什么?

我现在得到的是:

$html_template = @" 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<html> 
<head> 
    etc etc etc 
"@ 

# Simulate how ever you are querying for data. 
$desktop_path = "$env:userprofile\Desktop\" 
$filename = [Environment]::UserName 

# Does nothing only import 
$content = Import-CSV -Path D:\export.csv 

# Input fields 
$name= Read-Host -Prompt 'Input name (eg. Tim Tom)' 
$title= Read-Host -Prompt 'Input the job title (eg. Office Manager)' 
$phone= Read-Host -Prompt 'Input the last 3 telephone numbers (eg. 111)' 
$email= Read-Host -Prompt 'Input email adress (eg. [email protected])' 

# Check if map Handtekeningen exist else force create it. 
New-Item -ItemType Directory -Force -Path C:\Users\$filename\AppData\Roaming\Microsoft\Handtekeningen | Out-Null 

# Create html file 
$html_template -f $name,$title,$phone,$email,$filename | Out-File -Force $desktop_path\$filename.html 

# Set file to Readonly (problem is it cannot be overwriten) 
Set-ItemProperty $desktop_path\$filename.html -name IsReadOnly -value $true 

# Write 
Write-Host "Saving signature." 
Write-Host "Closing in 5 seconds." 

# Sleep for 5 seconds 
Start-Sleep -s 5 

CSV:

username;Full Name;Job Title;Telephone;Email 
+0

这部分没有什么意义。您的CSV文件为空,您可以导入CSV文件,但直接在后面查询用户的值。但总的来说,您使用的方法看起来很好,即使用占位符(本例中为.NET格式字符串)定义模板,获取需要插入的数据,填写模板并将其写入某处。我用过很多次类似的代码。那么你究竟在哪里遇到问题?尤其是CSV部分对我来说还不清楚 - 你想创建一个签名吗?还是很多呢? – Joey

+0

我知道我想要什么,但不知道如何去做,我从来没有使用过csv文件,我只需要读取与用户名匹配的1行。我只需要一小步 – Hoaxr

+0

我想为运行它的用户创建1,并且所有用户都在名为电子邮件电话等的csv文件中。 – Hoaxr

回答

1

如果(你的意见后)我理解正确的,你只需要弄清楚从CSV使用什么行文件。在这种情况下,你可以从环境变量USERNAME阅读的用户名和简单地过滤适当:

$content = Import-CSV -Path D:\export.csv 

$user = $content | where username -eq $Env:USERNAME 

if (!$user -or $user.Count -gt 1) { 
    throw 'No user, or multiple users found' 
} 

# Input fields 
$name= $user.'Full Name' 
$title= $user.'Job Title' 
$phone= $user.Telephone 
$email= $user.'Email' 
+0

男人你是我的英雄!工作就是我想让它工作!谢谢有一个愉快的周末:) – Hoaxr